Python Django HttpResponse Excel

Python Django HttpResponse Excel,python,django,python-2.7,xlsxwriter,Python,Django,Python 2.7,Xlsxwriter,我试图在Django网站上生成一个excel文件,所以我搜索了它并查看了它。我只编写了一个函数,将所需内容写入Excel文件 def create_excel(personal_information): output = StringIO.StringIO() book = xlsxwriter.Workbook(output) sheet = book.add_worksheet() if personal_information['name']:

我试图在Django网站上生成一个excel文件,所以我搜索了它并查看了它。我只编写了一个函数,将所需内容写入Excel文件

def create_excel(personal_information):

    output = StringIO.StringIO()
    book = xlsxwriter.Workbook(output)

    sheet = book.add_worksheet()

    if personal_information['name']:
        sheet.write(1, 1,  personal_information['name'], text_format)

    book.close() 
    output.seek(0)

    return output
在我看来.py

def export(request):
    personal_information = json.loads(request.POST.get('personal_data'))

    output = create_excel(personal_information)
    response = HttpResponse(output.read(), content_type="application/ms-excel")
    response['Content-Disposition'] = 'attachment; filename=Excel.xls'

    return response
然而,结果是“没有”。你有什么办法解决我的问题吗

谢谢。

试试这个: 在函数“创建excel”中:

output      = io.BytesIO()
workbook    = xlsxwriter.Workbook(output)
      .... your code .....
           at the end of your function
 # close workbook
workbook.close()
xlsx_data = output.getvalue()
return xlsx_data
在你看来:

if request.method == 'POST':
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; 
filename=your_template_name.xlsx'
    xlsx_data = create_excel()
    response.write(xlsx_data)

我有一些解决办法。我有一个文件,我在那里做这件事。我使用
openpyxl
,但如果需要保留宏(
.xlsm
),您也可以使用其他库,如
xlwings

以下是我的代码片段:

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

    # create workbook
    wb = Workbook()
    sheet = wb.active

    # stylize header row
    # 'id','title', 'quantity','pub_date'

    c1 = sheet.cell(row = 1, column = 1) 
    c1.value = "id"
    c1.font = Font(bold=True)

    c2 = sheet.cell(row= 1 , column = 2) 
    c2.value = "title"
    c2.font = Font(bold=True)

    c3 = sheet.cell(row= 1 , column = 3) 
    c3.value = "quantity"
    c3.font = Font(bold=True)

    c4 = sheet.cell(row= 1 , column = 4) 
    c4.value = "pub_date"
    c4.font = Font(bold=True)

    # export data to Excel
    rows = models.Data.objects.all().values_list('id','category', 'quantity','pub_date',)
    for row_num, row in enumerate(rows, 1):
        # row is just a tuple
        for col_num, value in enumerate(row):
            c5 = sheet.cell(row=row_num+1, column=col_num+1) 
            c5.value = value

    wb.save(response)

    return response

如果在呈现响应之前打印输出,会发生什么情况?输出是否正确形成?另外,您的
返回响应
语句缩进是否正确?这似乎不是问题…@rnevius是的,我无法正确复制和粘贴代码,但在我的原始代码中,没有缩进问题。当我在返回之前打印输出时,它给出“”。当我调试它时,我可以看到一些未知数字的响应内容。我还需要说,我正在尝试从弹出窗口中的按钮获取excel文件。我的意思是,也许我需要处理它?@waterkinq您确定
返回响应的缩进正确吗?您在浏览器中看到返回的响应是什么?:)这里是返回响应当我从Google Chrome网络查看时,我看到这样的响应;“PKÖnHoïT°[mxl/worksheets/sheet1.xmlQo0è÷),?/p$@VU'=Lāv{vÀTÀ200; vîÛ8…”