django使用pandas下载excel文件

Posted shouwangrenjian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django使用pandas下载excel文件相关的知识,希望对你有一定的参考价值。

最开始是将数据库数据写到本地excel中,再读出来返回给前端,后面发现可以使用BytesIO(),不用再使用本地文件

x_io = BytesIO()
df = pd.DataFrame(list(res[data]))
df.to_excel(x_io, sheet_name=table_name, index=False)
excel_name = table_name + .xlsx
response = HttpResponse()
response[Content-Type] = application/octet-stream
response[Content-Disposition] = attachment;filename="%s" % excel_name
response.write(x_io.getvalue())
return response

 

以上是关于django使用pandas下载excel文件的主要内容,如果未能解决你的问题,请参考以下文章