使用 django 和 HttpResponse 导出 xls 文件供用户下载时出现问题

发布于 2024-11-19 17:41:45 字数 1980 浏览 3 评论 0原文

我目前正在使用 xlwt 创建一个电子表格,并尝试将其导出为 django 中的 HttpResponse 供用户下载。我的代码看起来像这样:

response = HttpResponse(mimetype = "application/vnd.ms-excel")
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'
work_book.save(response)
return response

这似乎是正确的方法,但我得到了:(

Traceback (most recent call last):
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1233, in communicate
    req.respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 745, in respond
    self.server.gateway(self).respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1927, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\servers\basehttp.py", line 674, in __call__
    return self.application(environ, start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\handlers\wsgi.py", line 252, in __call__
    response = middleware_method(request, response)
  File "C:\dev\workspace-warranty\imcom\imcom\seo_mod\middleware.py", line 33, in process_response
    response.content = strip_spaces_between_tags(response.content.strip())
  File "C:\dev\workspace-warranty\3rdparty\django\utils\functional.py", line 259, in wrapper
    return func(*args, **kwargs)
  File "C:\dev\workspace-warranty\3rdparty\django\utils\html.py", line 89, in strip_spaces_between_tags
    return re.sub(r'>\s+<', '><', force_unicode(value))
  File "C:\dev\workspace-warranty\3rdparty\django\utils\encoding.py", line 88, in force_unicode
    raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte. You passed in 

我省略了其余部分,因为我得到了很长的一行 \xd0\xcf\x11\xe0\xa1\ xb1\x1a\xe1\x00 之类的东西)

你们对可能出现问题的事情有什么想法吗?是因为我的一些写入值如下所示:

work_sheet.write(r,#,information) 其中信息未转换为unicode?

I'm currently creating a spreadsheet using xlwt and trying to export it out as an HttpResponse in django for a user to download. My code looks like this:

response = HttpResponse(mimetype = "application/vnd.ms-excel")
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'
work_book.save(response)
return response

Which seems to be the right way to do it, but I'm getting a:

Traceback (most recent call last):
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1233, in communicate
    req.respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 745, in respond
    self.server.gateway(self).respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1927, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\servers\basehttp.py", line 674, in __call__
    return self.application(environ, start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\handlers\wsgi.py", line 252, in __call__
    response = middleware_method(request, response)
  File "C:\dev\workspace-warranty\imcom\imcom\seo_mod\middleware.py", line 33, in process_response
    response.content = strip_spaces_between_tags(response.content.strip())
  File "C:\dev\workspace-warranty\3rdparty\django\utils\functional.py", line 259, in wrapper
    return func(*args, **kwargs)
  File "C:\dev\workspace-warranty\3rdparty\django\utils\html.py", line 89, in strip_spaces_between_tags
    return re.sub(r'>\s+<', '><', force_unicode(value))
  File "C:\dev\workspace-warranty\3rdparty\django\utils\encoding.py", line 88, in force_unicode
    raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte. You passed in 

(I left off the rest because I get a really long line of this \xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00 kind of stuff)

Do you guys have any ideas on something that could be wrong with this? Is is it because some of my write values look like this:

work_sheet.write(r,#,information) where information isn't cast to unicode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-11-26 17:41:46
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'

.xls 周围应该

response['Content-Disposition'] = 'attachment; filename = %s.xls' % u'Zinnia_Entries'

没有引号,否则输出将是

u'attachment; filename = Zinnia_Entries +".xls"'

所以尝试更改它。

但也请查看这个答案。它有一个非常有用的小功能,用于输出 xls 文件。

django excel xlwt

response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'

should just be

response['Content-Disposition'] = 'attachment; filename = %s.xls' % u'Zinnia_Entries'

without quotes around .xls otherwise the output will be

u'attachment; filename = Zinnia_Entries +".xls"'

So try changing that.

But also check out this answer. It has a really helpful little function for outputing xls files.

django excel xlwt

溺ぐ爱和你が 2024-11-26 17:41:46

解决了问题。显然有人在其中放入了一些时髦的中间件东西,这些东西是分解、附加和添加等。等等。到文件。当不应该的时候。

无论如何,随着它的消失,文件导出完美。

@Storm - 感谢您的所有帮助!

Solved the problem. Apparently someone had put some funky middleware stuff in that was hacking apart and appending and adding, ect. ect. to the file. When it shouldn't.

Anyway, with it gone the file exports perfectly.

@Storm - Thank you for all the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文