在 Internet Explorer 中使用 django 和 xlwt 提供动态生成的 MS Excel 文件失败
我正在尝试使用 xlwt 从我的 django 站点上的数据库内容创建 MS-Excel 文件。
我在 stackoverflow 上看到了几个解决方案,特别是这个链接: django excel xlwt
和这个 django 片段: http://djangosnippets.org/snippets/2233/
这些示例适用于 Firefox,但不适用于互联网浏览器。屏幕上没有提示打开或保存文件,而是出现一堆飘忽不定的垃圾。看来IE认为响应是html。
这是我的视图函数:
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
我在 IE 8 中看到了这种行为。
对于为什么这在 Internet Explorer 中不起作用有什么建议吗?
谢谢。
I am trying to use xlwt to create MS-Excel files from the contents of the database on my django site.
I have seen several solutions here on stackoverflow, in particular this link: django excel xlwt
and this django snippet: 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.
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.
Any suggestions as to why this isn't working in Internet Explorer?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的
application/ms-excel
mimetype 对于.xls
文件无效。标准的是
application/vnd.ms-excel
看这里 为 Excel 文档设置 MIME 类型了解更多信息。
The mimetype you're using
application/ms-excel
is invalid for.xls
files.The standard one is
application/vnd.ms-excel
Look here Setting mime type for excel document for more informations.