我正在使用 openpyxl 创建一个 excel 文件,我想将其作为文件下载返回(因此不保存在本地)。
我可以很好地创建 excel 文件并将其保存到磁盘。但是,我无法下载此文件。
尝试1:
import flask_excel as excel
...
create_excel_sheet(data) # internally save the sheet with name sheet.xlsx
output = excel.make_response()
output.headers["Content-Disposition"] = "attachment; filename=" + \
'sheet.xlsx'
output.headers["Content-type"] = "application/vnd.openxmlformats-\
officedocument.spreadsheetml.sheet"
return output
这将返回一个名为 sheet.xlsx 的空文本文件
尝试 2: wb = create_excel_sheet(data) # return openpyxl workbook
output = excel.make_response(wb)
output.headers["Content-Disposition"] = "attachment; filename=" + \
'sheet.xlsx'
output.headers["Content-type"] = "application/vnd.openxmlformats-\
officedocument.spreadsheetml.sheet"
return output
我不想将 pyexcel 用于数据,因为我需要 openpyxl 来创建精美的 excel 表。显然,如果 pyexcel 和 openpyxl 进行通信就可以了。
有什么想法吗?
干杯,迈克