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