本文介绍了XlsxWriter对象保存为http响应,以在Django中创建下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XlsxWriter对象保存为http响应以在Django中创建下载
XlsxWriter object save as http response to create download in Django?
推荐答案
我想你在问如何创建使用 xlsxwriter
的内存中的excel文件,并通过 HttpResponse
返回。这里有一个例子:
I think you're asking about how to create an excel file in memory using xlsxwriter
and return it via HttpResponse
. Here's an example:
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
希望这有助于。
这篇关于XlsxWriter对象保存为http响应,以在Django中创建下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!