Fix historical pagination and bound export and proxy memory
This commit is contained in:
+45
-18
@@ -2,6 +2,7 @@
|
||||
import csv
|
||||
import io
|
||||
import json
|
||||
from contextlib import suppress
|
||||
import sqlite3
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
@@ -94,11 +95,18 @@ def import_file(path, year=None):
|
||||
book = load_workbook(path, read_only=True, data_only=True)
|
||||
try:
|
||||
sheet = book.active
|
||||
if sheet.max_row > 300001 or sheet.max_column > 100:
|
||||
if (sheet.max_row or 0) > 300001 or (sheet.max_column or 0) > 100:
|
||||
raise ValueError("Excel 超过 30 万行或 100 列,请拆分后导入")
|
||||
values = sheet.iter_rows(values_only=True)
|
||||
headers = next(values, [])
|
||||
rows = [dict(zip(headers, row)) for row in values if any(v is not None for v in row)]
|
||||
if len(headers) > 100:
|
||||
raise ValueError("Excel 超过 100 列,请拆分后导入")
|
||||
rows = []
|
||||
for index, row in enumerate(values, 2):
|
||||
if index > 300001 or len(row) > 100:
|
||||
raise ValueError("Excel 超过 30 万行或 100 列,请拆分后导入")
|
||||
if any(v is not None for v in row):
|
||||
rows.append(dict(zip(headers, row)))
|
||||
finally:
|
||||
book.close()
|
||||
else:
|
||||
@@ -179,28 +187,47 @@ def spreadsheet_value(value):
|
||||
|
||||
|
||||
def export_sheet(records, kind):
|
||||
rows = [[spreadsheet_value(record.get(field)) for field in LABELS] for record in records]
|
||||
if kind == "csv":
|
||||
stream = io.StringIO(newline="")
|
||||
writer = csv.writer(stream)
|
||||
writer.writerow(list(LABELS.values()))
|
||||
writer.writerows(rows)
|
||||
writer.writerows([spreadsheet_value(record.get(field)) for field in LABELS] for record in records)
|
||||
return stream.getvalue().encode("utf-8-sig")
|
||||
book = Workbook()
|
||||
sheet = book.active
|
||||
sheet.title = "院校专业"
|
||||
sheet.append(list(LABELS.values()))
|
||||
for row in rows:
|
||||
sheet.append(row)
|
||||
sheet.freeze_panes = "A2"
|
||||
sheet.auto_filter.ref = sheet.dimensions
|
||||
output = io.BytesIO()
|
||||
write_xlsx(records, output)
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def write_xlsx(records, output):
|
||||
from openpyxl.cell import WriteOnlyCell
|
||||
from openpyxl.utils import get_column_letter
|
||||
from openpyxl.styles import Font, PatternFill, Alignment
|
||||
for cell in sheet[1]:
|
||||
book = Workbook(write_only=True)
|
||||
sheet = book.create_sheet("院校专业")
|
||||
sheet.freeze_panes = "A2"
|
||||
header = []
|
||||
for index, label in enumerate(LABELS.values(), 1):
|
||||
cell = WriteOnlyCell(sheet, value=label)
|
||||
cell.font = Font(color="FFFFFF", bold=True)
|
||||
cell.fill = PatternFill("solid", fgColor="183447")
|
||||
cell.alignment = Alignment(vertical="center")
|
||||
for col in sheet.columns:
|
||||
sheet.column_dimensions[col[0].column_letter].width = 24
|
||||
output = io.BytesIO()
|
||||
book.save(output)
|
||||
return output.getvalue()
|
||||
header.append(cell)
|
||||
sheet.column_dimensions[get_column_letter(index)].width = 24
|
||||
count = 1
|
||||
try:
|
||||
sheet.append(header)
|
||||
for record in records:
|
||||
sheet.append([spreadsheet_value(record.get(field)) for field in LABELS])
|
||||
count += 1
|
||||
sheet.auto_filter.ref = f'A1:{get_column_letter(len(LABELS))}{count}'
|
||||
book.save(output)
|
||||
except BaseException:
|
||||
with suppress(Exception):
|
||||
if not sheet.closed:
|
||||
sheet.close()
|
||||
with suppress(Exception):
|
||||
if sheet._writer:
|
||||
sheet._writer.cleanup()
|
||||
raise
|
||||
finally:
|
||||
book.close()
|
||||
|
||||
Reference in New Issue
Block a user