Python XlsxWriter 对象另存为 http 响应以在 Django 中创建下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16393242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XlsxWriter object save as http response to create download in Django
提问by Waheed Ahmed
XlsxWriter object save as http response to create download in Django?
XlsxWriter 对象另存为 http 响应以在 Django 中创建下载?
采纳答案by alecxe
I think you're asking about how to create an excel file in memory using xlsxwriterand return it via HttpResponse. Here's an example:
我想您是在询问如何在内存中使用创建一个 excel 文件xlsxwriter并通过HttpResponse. 下面是一个例子:
try:
import cStringIO as StringIO
except ImportError:
import StringIO
from django.http import HttpResponse
from xlsxwriter.workbook import Workbook
def your_view(request):
# your view logic here
# create a workbook in memory
output = StringIO.StringIO()
book = Workbook(output)
sheet = book.add_worksheet('test')
sheet.write(0, 0, 'Hello, world!')
book.close()
# construct response
output.seek(0)
response = HttpResponse(output.read(), mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = "attachment; filename=test.xlsx"
return response
Hope that helps.
希望有帮助。
回答by Jeb
A little update on @alecxe response for Python 3 (io.BytesIOinstead of StringIO.StringIO) and Django >= 1.5 (content_typeinstead of mimetype), with the fully in-memory file assembly that has since been implemented by @jmcnamara ({'in_memory': True}) !
Here is the full example :
对 Python 3(io.BytesIO而不是StringIO.StringIO)和 Django >= 1.5(content_type而不是mimetype)的@alecxe 响应的一些更新,以及此后由@jmcnamara 实现的完全内存文件程序集({ 'in_memory': 真}) !
这是完整的示例:
import io
from django.http.response import HttpResponse
from xlsxwriter.workbook import Workbook
def your_view(request):
output = io.BytesIO()
workbook = Workbook(output, {'in_memory': True})
worksheet = workbook.add_worksheet()
worksheet.write(0, 0, 'Hello, world!')
workbook.close()
output.seek(0)
response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = "attachment; filename=test.xlsx"
output.close()
return response
回答by nuts
When it comes to Django, you can even do without the whole StringIOshenanigans. HttpResponsebehaves just like a StringIO in that respect:
谈到 Django,您甚至可以不用整个StringIO恶作剧。HttpResponse在这方面就像 StringIO 一样:
from django.http import HttpResponse
from xlsxwriter.workbook import Workbook
def your_view(request):
# your view logic here
# create the HttpResponse object ...
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = "attachment; filename=test.xlsx"
# .. and pass it into the XLSXWriter
book = Workbook(response, {'in_memory': True})
sheet = book.add_worksheet('test')
sheet.write(0, 0, 'Hello, world!')
book.close()
return response
Addendum: You need to specify {'in_memory': True}or you might get HttpResponse has no attribute seek(). Thanks @Jeb
附录:您需要指定,{'in_memory': True}否则您可能会得到HttpResponse has no attribute seek(). 谢谢@杰布

