项目使用的是 uwsgi
+ Django
from django.http import HttpResponse
from openpyxl import Workbook, load_workbook
from openpyxl.writer.excel import save_virtual_workbook
class ExcelTableObj(object):
def __init__(self, file_name=None, write_only=False):
if file_name:
self.file_name = file_name
self.wb = load_workbook(file_name)
else:
self.wb = Workbook(write_only=write_only)
self.write_only = write_only
def fix_save_excel(self, header, sheet_name, data_lists):
ws = self.wb.create_sheet(title=sheet_name)
ws.append(header)
for data in data_lists:
ws.append(data)
# Django view
excel = ExcelTableObj(write_only=True)
try:
excel.fix_save_excel(title, sheetname, data_lists)
except Exception as e:
import traceback as t; t.print_exc()
logger.error(repr(e))
finally:
response = HttpResponse(save_virtual_workbook(excel.wb),
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename={}'.format(filename)
因为 excel 文件很大, 所以使用write_only
模式
lxml
模块之后, 内存消耗稳定, 但是最后的文件打开异常 好像 uwsgi+lxml 会影响 openpyxl excel 文件的生成
lxml
模块, 内存一直往上涨, 最后文件正常,但是太耗内存请大佬帮忙看看原因, 以及可以优化的点, 非常感谢
感谢之前问题中 @superrichman 大佬的建议
1
omph 2021-03-23 17:56:05 +08:00
不考虑向作者反馈重现一下?
|
2
xchaoinfo 2021-03-23 18:23:40 +08:00 via Android
写入用 xlsxwriter
|