在Internet Explorer中使用django和xlwt提供动态生成的MS Excel文件失败

时间:2021-08-22 20:24:38

I am trying to use xlwt to create MS-Excel files from the contents of the database on my django site.

我正在尝试使用xlwt从我的django站点上的数据库内容创建MS-Excel文件。

I have seen several solutions here on *, in particular this link: django excel xlwt

我在*上看到了几个解决方案,特别是这个链接:django excel xlwt

and this django snippet: http://djangosnippets.org/snippets/2233/

和这个django片段:http://djangosnippets.org/snippets/2233/

These examples work in firefox, but not in Internet Explorer. Instead of getting prompted to open or save a file, a bunch of wingding junk appears on the screen. It seems that IE thinks the response is html.

这些示例适用于Firefox,但不适用于Internet Explorer。屏幕上出现了一堆蜿蜒的垃圾,而不是被提示打开或保存文件。 IE似乎认为响应是html。

Here is my view function:

这是我的视图功能:

def exportexcel(request):
    from xlwt import Workbook

    wb = Workbook()
    ws = wb.add_sheet('Sheetname')
    ws.write(0, 0, 'Firstname')
    ws.write(0, 1, 'Surname')
    ws.write(1, 0, 'Hans')
    ws.write(1, 1, 'Muster')

    fname = 'testfile.xls'
    response = HttpResponse(mimetype="application/ms-excel")
    response['Content-Disposition'] = 'attachment; filename=%s' % fname

    wb.save(response)

    return response

I am seeing this behavior in IE 8.

我在IE 8中看到了这种行为。

Any suggestions as to why this isn't working in Internet Explorer?

有关为什么这在Internet Explorer中不起作用的任何建议?

Thanks.

1 个解决方案

#1


4  

The mimetype you're using application/ms-excel is invalid for .xls files.

您正在使用application / ms-excel的mimetype对.xls文件无效。

The standard one is application/vnd.ms-excel

标准的是application / vnd.ms-excel

Look here Setting mime type for excel document for more informations.

查看此处为excel文档设置mime类型以获取更多信息。

#1


4  

The mimetype you're using application/ms-excel is invalid for .xls files.

您正在使用application / ms-excel的mimetype对.xls文件无效。

The standard one is application/vnd.ms-excel

标准的是application / vnd.ms-excel

Look here Setting mime type for excel document for more informations.

查看此处为excel文档设置mime类型以获取更多信息。