整齐的包装!我不知道这个
根据文档,该save(filename_or_stream)
方法采用要保存的文件名或要写入的类似文件的流。
而Django响应对象恰好是一个类似文件的流!所以做吧xls.save(response)
。查看有关使用ReportLab 生成PDF的Django文档,以了解类似情况。
编辑:(改编自肖恩·米洛的评论):
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')