import { downloadCsv, downloadExcelData } from './excelImportExport'; export type ExportFormat = 'xlsx' | 'csv' | 'print'; export type ExportLoadOptions = { expandItems: boolean; customerCode?: string; }; export type ExportColumnGroup = 'header' | 'item' | 'summary'; export interface ExportColumnDef> { id: string; label: string; group?: ExportColumnGroup; defaultSelected?: boolean; getValue: (row: T) => string | number; } export interface DocumentExportOptions> { format: ExportFormat; filename: string; sheetName: string; title: string; columns: ExportColumnDef[]; selectedColumnIds: string[]; rows: T[]; meta?: Record; /** 打印时标题是否居中,默认 true */ titleCentered?: boolean; /** 为 false 时不再在文件名后追加当天日期(调用方已带筛选时段) */ appendDateStamp?: boolean; } export function getDefaultSelectedColumnIds(columns: ExportColumnDef[]): string[] { return columns.filter((col) => col.defaultSelected !== false).map((col) => col.id); } export function buildExportMatrix( columns: ExportColumnDef[], selectedColumnIds: string[], rows: T[] ): { headers: string[]; matrix: (string | number)[][] } { const active = columns.filter((col) => selectedColumnIds.includes(col.id)); const headers = active.map((col) => col.label); const matrix = rows.map((row) => active.map((col) => { const value = col.getValue(row); return value === null || value === undefined ? '' : value; }) ); return { headers, matrix }; } function escapeHtml(value: unknown): string { return String(value ?? '') .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } export function buildExportTableHtml(options: { title: string; headers: string[]; rows: (string | number)[][]; meta?: Record; autoPrint?: boolean; /** 标题是否居中,默认 true */ titleCentered?: boolean; }): string { const metaHtml = options.meta ? Object.entries(options.meta) .map(([key, value]) => `
${escapeHtml(key)}${escapeHtml(value)}
`) .join('') : ''; const tableHead = options.headers.map((h) => `${escapeHtml(h)}`).join(''); const tableBody = options.rows .map( (row) => `${row.map((cell) => `${escapeHtml(cell)}`).join('')}` ) .join(''); const autoPrintScript = options.autoPrint === false ? '' : ''; const titleAlign = options.titleCentered === false ? 'left' : 'center'; return `${escapeHtml(options.title)}

${escapeHtml(options.title)}

${metaHtml ? `
${metaHtml}
` : ''} ${tableHead}${tableBody}
${autoPrintScript} `; } export function printExportTable(options: { title: string; headers: string[]; rows: (string | number)[][]; meta?: Record; titleCentered?: boolean; }) { const html = buildExportTableHtml({ ...options, autoPrint: true }); const win = window.open('', '_blank'); if (!win) { throw new Error('无法打开打印窗口,请允许浏览器弹窗'); } win.document.write(html); win.document.close(); } export async function executeDocumentExport(options: DocumentExportOptions) { const { headers, matrix } = buildExportMatrix( options.columns, options.selectedColumnIds, options.rows ); if (headers.length === 0) { throw new Error('请至少选择一列'); } if (matrix.length === 0) { throw new Error('没有可导出的数据'); } const stamp = new Date().toISOString().slice(0, 10); const baseName = options.filename.replace(/\.(xlsx|csv)$/i, ''); const datedName = options.appendDateStamp === false ? baseName : `${baseName}_${stamp}`; if (options.format === 'print') { printExportTable({ title: options.title, headers, rows: matrix, meta: { ...options.meta, 导出时间: new Date().toLocaleString('zh-CN'), 记录数: String(matrix.length), }, titleCentered: options.titleCentered, }); return; } if (options.format === 'csv') { downloadCsv(`${datedName}.csv`, headers, matrix); return; } await downloadExcelData(`${datedName}.xlsx`, options.sheetName, headers, matrix); }