【发布时间】:2011-08-26 19:53:34
【问题描述】:
我正在尝试使用 xlwt 从我的 django 站点上的数据库内容创建 MS-Excel 文件。
我在 stackoverflow 上看到了几个解决方案,特别是这个链接:django excel xlwt
还有这个 django sn-p:http://djangosnippets.org/snippets/2233/
这些示例在 Firefox 中有效,但在 Internet Explorer 中无效。屏幕上没有提示打开或保存文件,而是出现了一堆乱七八糟的垃圾。好像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 中不起作用的任何建议?
谢谢。
【问题讨论】:
-
试试
application/vnd.ms-excelmimetype。 -
哇,这速度很快,而且很有效。谢谢你。你能解释一下'vnd'是做什么的吗?
-
看我的答案解释。
标签: django internet-explorer-8 xlwt