【问题标题】:Serving a dynamically generated MS Excel files using django and xlwt fails in Internet Explorer在 Internet Explorer 中使用 django 和 xlwt 提供动态生成的 MS Excel 文件失败
【发布时间】: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-excel mimetype。
  • 哇,这速度很快,而且很有效。谢谢你。你能解释一下'vnd'是做什么的吗?
  • 看我的答案解释。

标签: django internet-explorer-8 xlwt


【解决方案1】:

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

标准的是application/vnd.ms-excel

查看这里Setting mime type for excel document了解更多信息。

【讨论】:

  • 尽管如此,Content-Disposition 标头应该会导致保存提示。 OP 应该使用 Fiddler 查看 HTTP 响应标头以确保标头正在发送。
猜你喜欢
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 2011-12-17
  • 2010-11-07
  • 2012-03-25
  • 2012-10-23
  • 2013-02-23
  • 1970-01-01
相关资源
最近更新 更多