整洁的包装!我不知道这件事
根据文档,save(filename_or_stream)
方法要么使用文件名保存,要么使用类似文件的流写入.
而Django响应对象恰好是一个类似文件的流!所以只要做xls.save(response)
就行了.使用ReportLab查看Django文档中的大约generating PDFs个,可以看到类似的情况.
edit:(改编自ShawnMilo的 comments ):
def xls_to_response(xls, fname):
response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=%s' % fname
xls.save(response)
return response
然后,从您的视图函数中,只需创建xls
对象并以
return xls_to_response(xls,'foo.xls')