码迷,mamicode.com
首页 > 其他好文 > 详细

django使用pandas下载excel文件

时间:2020-02-25 20:32:19      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:type   bytes   get   div   val   excel   使用   panda   pandas   

最开始是将数据库数据写到本地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文件

标签:type   bytes   get   div   val   excel   使用   panda   pandas   

原文地址:https://www.cnblogs.com/shouwangrenjian/p/12363088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!