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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM