最開始是將數據庫數據寫到本地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