Python Django 下载模型为Excel文件

2024-04-25 17:59:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据的数据库,我想下载它作为一个excel文件。换句话说,当我点击应用程序上的下载按钮时,如何以编程方式将模型转换为excel文件?在


Tags: 文件数据模型数据库应用程序编程方式excel
1条回答
网友
1楼 · 发布于 2024-04-25 17:59:26

使用优秀的xlsxwriter库。我用过django的,非常棒 容易的。在

from django.http import HttpResponse
import xlsxwriter

def returnexcel():
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="mymodel.xlsx"'

    workbook = xlsxwriter.Workbook(response, {'in_memory': True})

# add a worksheet

    worksheet = workbook.add_worksheet()
    worksheet.write_... 

# use xlsxwriter routines to create the worksheet

# now write it out
    workbook.close()
    return response

http://xlsxwriter.readthedocs.io/index.html

相关问题 更多 >