- 原实现:明细一条大 SQL 全量拉回(货主 1000305 单次 16.9 万行),
SheetJS aoa_to_sheet 全内存构建 490 万单元格后一次性 XLSX.write 序列化,
生产实测总耗时 81.7s(其中 Excel 生成约 30~50s、内存峰值约 2GB),
且进度只有 10/100 两档,中间 80s 零反馈被误判卡死
- 改造(对齐 order_export 流式架构):
1) 明细按主键 id 游标分批拉取(batchSize=5000,走 PRIMARY 索引,
生产实测每批约 95ms、末批不满即止无空探测),行序由 created_at,id
变更为 id 升序(自增序≈插入序)
2) ExcelJS.stream.xlsx.WorkbookWriter 流式落盘(useStyles/useSharedStrings
关闭),逐批行级 commit,内存占用从约 2GB 降到批级
3) 富化映射(商品/货主/条码/入库单/订单/运单)改为按批加载
4) onProgress 每批上报 current_count/total_count,进度 5→95→100 平滑
- excel.js 新增 uploadExistingFile:流式生成场景复用上传链路
(归一化文件名/buildUploadKey/uploadFile/uploadSuccessNotify),
供 WorkbookWriter 落盘后 createReadStream 上传
- systemMethods 层 export/exportStore 接 onProgress 并持久化
current_count/total_count 字段(向前端兼容订单导出进度组件)
- 验证:storage_report_export.test.js 2/2 PASS(12000 行游标 3 批、
进度序列 5/95/100、回读 Excel 校验 sheet/表头/行数/单元格值、
空数据仅表头);生产只读探测 EXPLAIN 走 PRIMARY range、
首尾页约 95ms;ESLint 4 文件 0 error
945 lines
29 KiB
JavaScript
945 lines
29 KiB
JavaScript
'use strict';
|
||
|
||
const crypto = require('crypto');
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const { Readable } = require('stream');
|
||
const Service = require('egg').Service;
|
||
const XLSX = require('xlsx');
|
||
const ExcelJS = require('exceljs');
|
||
|
||
const CONTENT_TYPE_MAP = {
|
||
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||
xls: 'application/vnd.ms-excel',
|
||
csv: 'text/csv; charset=utf-8',
|
||
};
|
||
|
||
class ExcelService extends Service {
|
||
getContentType(bookType = 'xlsx') {
|
||
const normalizedType = String(bookType || 'xlsx').replace('.', '').toLowerCase();
|
||
return CONTENT_TYPE_MAP[normalizedType] || CONTENT_TYPE_MAP.xlsx;
|
||
}
|
||
|
||
sanitizeSheetName(name, index = 1) {
|
||
const safeName = String(name || `Sheet${index}`)
|
||
.replace(/[\\/?*\[\]:]/g, '_')
|
||
.trim();
|
||
|
||
return (safeName || `Sheet${index}`).slice(0, 31);
|
||
}
|
||
|
||
normalizeHeaders(headerRow = []) {
|
||
const usedNames = new Set();
|
||
|
||
return headerRow.map((value, index) => {
|
||
const baseName = String(value == null ? '' : value).trim() || `column_${index + 1}`;
|
||
let uniqueName = baseName;
|
||
let suffix = 2;
|
||
|
||
while (usedNames.has(uniqueName)) {
|
||
uniqueName = `${baseName}_${suffix}`;
|
||
suffix += 1;
|
||
}
|
||
|
||
usedNames.add(uniqueName);
|
||
return uniqueName;
|
||
});
|
||
}
|
||
|
||
normalizeColumns(columns = []) {
|
||
if (!Array.isArray(columns)) {
|
||
return [];
|
||
}
|
||
|
||
return columns
|
||
.map(column => {
|
||
if (!column) {
|
||
return null;
|
||
}
|
||
|
||
const key = column.key || column.field || column.dataIndex || column.prop;
|
||
const header = column.header || column.title || column.label || key;
|
||
|
||
if (!key && !header) {
|
||
return null;
|
||
}
|
||
|
||
return {
|
||
key: key || header,
|
||
header: header || key,
|
||
width: Number(column.width || column.wch || 0) || undefined,
|
||
};
|
||
})
|
||
.filter(Boolean);
|
||
}
|
||
|
||
prepareSheetData(sheetConfig = {}) {
|
||
const {
|
||
rows = [],
|
||
columns = [],
|
||
header = [],
|
||
skipHeader = false,
|
||
widths = [],
|
||
} = sheetConfig;
|
||
|
||
const normalizedColumns = this.normalizeColumns(columns);
|
||
let aoaData = [];
|
||
|
||
if (normalizedColumns.length > 0) {
|
||
const headerRow = normalizedColumns.map(column => column.header);
|
||
const columnKeys = normalizedColumns.map(column => column.key);
|
||
const bodyRows = Array.isArray(rows)
|
||
? rows.map(row => {
|
||
if (Array.isArray(row)) {
|
||
return row;
|
||
}
|
||
|
||
return columnKeys.map(key => (row && typeof row === 'object' ? row[key] : ''));
|
||
})
|
||
: [];
|
||
aoaData = skipHeader ? bodyRows : [ headerRow, ...bodyRows ];
|
||
} else if (Array.isArray(rows) && rows.length > 0 && Array.isArray(rows[0])) {
|
||
aoaData = Array.isArray(header) && header.length > 0 && !skipHeader
|
||
? [ header, ...rows ]
|
||
: rows;
|
||
} else if (Array.isArray(rows) && rows.length > 0) {
|
||
const derivedHeaders = Array.isArray(header) && header.length > 0
|
||
? header
|
||
: Object.keys(rows[0] || {});
|
||
const bodyRows = rows.map(row => derivedHeaders.map(key => (row && typeof row === 'object' ? row[key] : '')));
|
||
aoaData = skipHeader ? bodyRows : [ derivedHeaders, ...bodyRows ];
|
||
} else {
|
||
aoaData = Array.isArray(header) && header.length > 0 ? [ header ] : [];
|
||
}
|
||
|
||
const resolvedWidths = Array.isArray(widths) && widths.length > 0
|
||
? widths.map(item => (typeof item === 'number' ? { wch: item } : item))
|
||
: normalizedColumns
|
||
.filter(column => column.width)
|
||
.map(column => ({ wch: column.width }));
|
||
|
||
return {
|
||
normalizedColumns,
|
||
aoaData,
|
||
resolvedWidths,
|
||
skipHeader,
|
||
originalRows: Array.isArray(rows) ? rows : [],
|
||
};
|
||
}
|
||
|
||
hasEmbeddedImages(options = {}) {
|
||
return Array.isArray(options.sheets)
|
||
&& options.sheets.some(sheet => Array.isArray(sheet && sheet.embeddedImages) && sheet.embeddedImages.length > 0);
|
||
}
|
||
|
||
buildWorksheet(sheetConfig = {}) {
|
||
const {
|
||
merges = [],
|
||
textColumns = [],
|
||
} = sheetConfig;
|
||
const {
|
||
aoaData,
|
||
resolvedWidths,
|
||
} = this.prepareSheetData(sheetConfig);
|
||
|
||
const worksheet = XLSX.utils.aoa_to_sheet(aoaData);
|
||
|
||
if (resolvedWidths.length > 0) {
|
||
worksheet['!cols'] = resolvedWidths;
|
||
}
|
||
|
||
if (Array.isArray(merges) && merges.length > 0) {
|
||
worksheet['!merges'] = merges;
|
||
}
|
||
|
||
this.applyTextColumnsToSheetJs(worksheet, textColumns);
|
||
|
||
return worksheet;
|
||
}
|
||
|
||
buildWorkbook(options = {}) {
|
||
const { sheets = [], props = {} } = options;
|
||
|
||
if (!Array.isArray(sheets) || sheets.length === 0) {
|
||
throw new Error('至少需要一个 sheet 配置');
|
||
}
|
||
|
||
const workbook = XLSX.utils.book_new();
|
||
workbook.Props = {
|
||
Company: 'WMS',
|
||
CreatedDate: new Date(),
|
||
...props,
|
||
};
|
||
|
||
sheets.forEach((sheetConfig, index) => {
|
||
const worksheet = this.buildWorksheet(sheetConfig || {});
|
||
const sheetName = this.sanitizeSheetName(sheetConfig && sheetConfig.name, index + 1);
|
||
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
|
||
});
|
||
|
||
return workbook;
|
||
}
|
||
|
||
async buildExcelJsWorkbook(options = {}) {
|
||
const { sheets = [], props = {} } = options;
|
||
|
||
if (!Array.isArray(sheets) || sheets.length === 0) {
|
||
throw new Error('至少需要一个 sheet 配置');
|
||
}
|
||
|
||
const workbook = new ExcelJS.Workbook();
|
||
workbook.creator = props.author || 'WMS';
|
||
workbook.company = props.company || 'WMS';
|
||
workbook.created = props.createdAt || new Date();
|
||
workbook.modified = new Date();
|
||
|
||
for (let index = 0; index < sheets.length; index += 1) {
|
||
const sheetConfig = sheets[index] || {};
|
||
const sheetName = this.sanitizeSheetName(sheetConfig.name, index + 1);
|
||
const worksheet = workbook.addWorksheet(sheetName);
|
||
const {
|
||
merges = [],
|
||
embeddedImages = [],
|
||
styledRows = [],
|
||
textColumns = [],
|
||
} = sheetConfig;
|
||
const {
|
||
normalizedColumns,
|
||
aoaData,
|
||
resolvedWidths,
|
||
skipHeader,
|
||
originalRows,
|
||
} = this.prepareSheetData(sheetConfig);
|
||
|
||
if (normalizedColumns.length > 0 && originalRows.length > 0 && !Array.isArray(originalRows[0])) {
|
||
if (skipHeader) {
|
||
aoaData.forEach(row => worksheet.addRow(row));
|
||
} else {
|
||
worksheet.columns = normalizedColumns.map(column => ({
|
||
header: column.header,
|
||
key: column.key,
|
||
width: column.width || 18,
|
||
}));
|
||
originalRows.forEach(row => worksheet.addRow(row || {}));
|
||
}
|
||
} else if (normalizedColumns.length > 0 && originalRows.length === 0 && !skipHeader) {
|
||
worksheet.columns = normalizedColumns.map(column => ({
|
||
header: column.header,
|
||
key: column.key,
|
||
width: column.width || 18,
|
||
}));
|
||
} else {
|
||
aoaData.forEach(row => worksheet.addRow(row));
|
||
}
|
||
|
||
if (resolvedWidths.length > 0) {
|
||
resolvedWidths.forEach((column, columnIndex) => {
|
||
const width = Number(column && column.wch) || Number(column && column.width) || undefined;
|
||
if (width) {
|
||
worksheet.getColumn(columnIndex + 1).width = width;
|
||
}
|
||
});
|
||
}
|
||
|
||
if (Array.isArray(merges) && merges.length > 0) {
|
||
merges.forEach(merge => {
|
||
if (!merge) {
|
||
return;
|
||
}
|
||
|
||
if (typeof merge === 'string') {
|
||
worksheet.mergeCells(merge);
|
||
return;
|
||
}
|
||
|
||
if (merge.s && merge.e) {
|
||
worksheet.mergeCells(merge.s.r + 1, merge.s.c + 1, merge.e.r + 1, merge.e.c + 1);
|
||
}
|
||
});
|
||
}
|
||
|
||
if (Array.isArray(styledRows) && styledRows.length > 0) {
|
||
this.applyStyledRows(worksheet, styledRows);
|
||
}
|
||
|
||
if (Array.isArray(textColumns) && textColumns.length > 0) {
|
||
this.applyTextColumnsToExcelJs(worksheet, textColumns);
|
||
}
|
||
|
||
if (Array.isArray(embeddedImages) && embeddedImages.length > 0) {
|
||
await this.addEmbeddedImages(workbook, worksheet, embeddedImages);
|
||
}
|
||
}
|
||
|
||
return workbook;
|
||
}
|
||
|
||
inferImageExtension(imageConfig = {}, contentType = '') {
|
||
const preferred = String(imageConfig.extension || '').replace('.', '').toLowerCase();
|
||
if (preferred) {
|
||
return preferred === 'jpg' ? 'jpeg' : preferred;
|
||
}
|
||
|
||
const fileSource = imageConfig.url || imageConfig.filePath || imageConfig.filename || '';
|
||
const ext = path.extname(String(fileSource)).replace('.', '').toLowerCase();
|
||
if (ext) {
|
||
return ext === 'jpg' ? 'jpeg' : ext;
|
||
}
|
||
|
||
if (contentType.includes('png')) return 'png';
|
||
if (contentType.includes('gif')) return 'gif';
|
||
if (contentType.includes('jpg') || contentType.includes('jpeg')) return 'jpeg';
|
||
if (contentType.includes('webp')) return 'png';
|
||
|
||
return 'png';
|
||
}
|
||
|
||
async resolveImageSource(imageConfig = {}) {
|
||
try {
|
||
if (imageConfig.buffer) {
|
||
return {
|
||
buffer: Buffer.isBuffer(imageConfig.buffer) ? imageConfig.buffer : Buffer.from(imageConfig.buffer),
|
||
extension: this.inferImageExtension(imageConfig),
|
||
};
|
||
}
|
||
|
||
if (imageConfig.dataUrl) {
|
||
const match = String(imageConfig.dataUrl).match(/^data:(image\/[a-zA-Z0-9.+-]+);base64,(.+)$/);
|
||
if (match) {
|
||
return {
|
||
buffer: Buffer.from(match[2], 'base64'),
|
||
extension: this.inferImageExtension(imageConfig, match[1]),
|
||
};
|
||
}
|
||
}
|
||
|
||
if (imageConfig.base64) {
|
||
return {
|
||
buffer: Buffer.from(String(imageConfig.base64), 'base64'),
|
||
extension: this.inferImageExtension(imageConfig),
|
||
};
|
||
}
|
||
|
||
if (imageConfig.filePath) {
|
||
const resolvedFilePath = path.resolve(String(imageConfig.filePath));
|
||
const buffer = await fs.promises.readFile(resolvedFilePath);
|
||
return {
|
||
buffer,
|
||
extension: this.inferImageExtension({ ...imageConfig, filePath: resolvedFilePath }),
|
||
};
|
||
}
|
||
|
||
if (imageConfig.url) {
|
||
const imageUrl = String(imageConfig.url);
|
||
const dataUrlMatch = imageUrl.match(/^data:(image\/[a-zA-Z0-9.+-]+);base64,(.+)$/);
|
||
if (dataUrlMatch) {
|
||
return {
|
||
buffer: Buffer.from(dataUrlMatch[2], 'base64'),
|
||
extension: this.inferImageExtension(imageConfig, dataUrlMatch[1]),
|
||
};
|
||
}
|
||
|
||
if (this.ctx && typeof this.ctx.curl === 'function') {
|
||
const response = await this.ctx.curl(imageUrl, {
|
||
method: 'GET',
|
||
timeout: 15000,
|
||
followRedirect: true,
|
||
});
|
||
const contentType = String((response.headers && (response.headers['content-type'] || response.headers['Content-Type'])) || '').toLowerCase();
|
||
const buffer = Buffer.isBuffer(response.data) ? response.data : Buffer.from(response.data || '');
|
||
|
||
if (buffer.length > 0) {
|
||
return {
|
||
buffer,
|
||
extension: this.inferImageExtension(imageConfig, contentType),
|
||
};
|
||
}
|
||
} else if (typeof fetch === 'function' && /^https?:\/\//i.test(imageUrl)) {
|
||
const response = await fetch(imageUrl);
|
||
if (response && response.ok) {
|
||
const contentType = String(response.headers.get('content-type') || '').toLowerCase();
|
||
const arrayBuffer = await response.arrayBuffer();
|
||
const buffer = Buffer.from(arrayBuffer);
|
||
|
||
if (buffer.length > 0) {
|
||
return {
|
||
buffer,
|
||
extension: this.inferImageExtension(imageConfig, contentType),
|
||
};
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (error) {
|
||
if (this.ctx && this.ctx.logger && typeof this.ctx.logger.warn === 'function') {
|
||
this.ctx.logger.warn('[ExcelService.resolveImageSource] 图片加载失败: %s', error.message);
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
mergeCellStyle(baseStyle = {}, patchStyle = {}) {
|
||
const mergedStyle = {
|
||
...baseStyle,
|
||
...patchStyle,
|
||
};
|
||
|
||
if (baseStyle.font || patchStyle.font) {
|
||
mergedStyle.font = {
|
||
...(baseStyle.font || {}),
|
||
...(patchStyle.font || {}),
|
||
};
|
||
}
|
||
|
||
if (baseStyle.alignment || patchStyle.alignment) {
|
||
mergedStyle.alignment = {
|
||
...(baseStyle.alignment || {}),
|
||
...(patchStyle.alignment || {}),
|
||
};
|
||
}
|
||
|
||
if (baseStyle.protection || patchStyle.protection) {
|
||
mergedStyle.protection = {
|
||
...(baseStyle.protection || {}),
|
||
...(patchStyle.protection || {}),
|
||
};
|
||
}
|
||
|
||
if (patchStyle.fill) {
|
||
mergedStyle.fill = patchStyle.fill;
|
||
}
|
||
|
||
if (patchStyle.border) {
|
||
mergedStyle.border = patchStyle.border;
|
||
}
|
||
|
||
if (patchStyle.numFmt) {
|
||
mergedStyle.numFmt = patchStyle.numFmt;
|
||
}
|
||
|
||
return mergedStyle;
|
||
}
|
||
|
||
applyStyledRows(worksheet, styledRows = []) {
|
||
styledRows.forEach(item => {
|
||
if (!item) {
|
||
return;
|
||
}
|
||
|
||
const rowNumber = Math.max(Number(item.row || 1), 1);
|
||
const fromCol = Math.max(Number(item.fromCol || 1), 1);
|
||
const toCol = Math.max(Number(item.toCol || fromCol), fromCol);
|
||
const row = worksheet.getRow(rowNumber);
|
||
|
||
if (item.height) {
|
||
row.height = item.height;
|
||
}
|
||
|
||
if (item.style && typeof item.style === 'object') {
|
||
for (let col = fromCol; col <= toCol; col += 1) {
|
||
const cell = worksheet.getCell(rowNumber, col);
|
||
cell.style = this.mergeCellStyle(cell.style || {}, item.style);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* SheetJS 路径:将 textColumns 指定列落地为文本单元格。
|
||
* 单元格类型强制为文本(t:'s')、值转字符串,并附加文本数字格式(z:'@'),
|
||
* 防止长数字串被 Excel 按数值展示成科学计数法。
|
||
* @param {Object} worksheet SheetJS 工作表对象
|
||
* @param {Array<number>} [textColumns=[]] 0 基文本列索引列表
|
||
* @return {void} 无返回值
|
||
*/
|
||
applyTextColumnsToSheetJs(worksheet, textColumns = []) {
|
||
if (!worksheet || !Array.isArray(textColumns) || textColumns.length === 0) {
|
||
return;
|
||
}
|
||
|
||
const columnSet = new Set(
|
||
textColumns.map(index => Math.max(0, Math.floor(Number(index) || 0)))
|
||
);
|
||
const range = XLSX.utils.decode_range(worksheet['!ref'] || 'A1');
|
||
|
||
for (let row = range.s.r; row <= range.e.r; row += 1) {
|
||
for (const column of columnSet) {
|
||
if (column > range.e.c) {
|
||
continue;
|
||
}
|
||
const address = XLSX.utils.encode_cell({ r: row, c: column });
|
||
const cell = worksheet[address];
|
||
if (!cell) {
|
||
continue;
|
||
}
|
||
if (cell.v !== null && cell.v !== undefined) {
|
||
cell.t = 's';
|
||
cell.v = String(cell.v);
|
||
}
|
||
cell.z = '@';
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* ExcelJS 路径:将 textColumns 指定列落地为文本单元格。
|
||
* 单元格值转为字符串(原始对象类型除外),并设置文本数字格式 numFmt='@',
|
||
* 防止长数字串被 Excel 按数值展示成科学计数法。
|
||
* @param {Object} worksheet ExcelJS 工作表对象
|
||
* @param {Array<number>} [textColumns=[]] 0 基文本列索引列表
|
||
* @return {void} 无返回值
|
||
*/
|
||
applyTextColumnsToExcelJs(worksheet, textColumns = []) {
|
||
if (!worksheet || !Array.isArray(textColumns) || textColumns.length === 0) {
|
||
return;
|
||
}
|
||
|
||
const columnSet = new Set(
|
||
textColumns.map(index => Math.max(1, Math.floor(Number(index) || 0) + 1))
|
||
);
|
||
|
||
for (let row = 1; row <= worksheet.rowCount; row += 1) {
|
||
worksheet.getRow(row).eachCell({ includeEmpty: false }, (cell, columnNumber) => {
|
||
if (!columnSet.has(columnNumber)) {
|
||
return;
|
||
}
|
||
const value = cell.value;
|
||
if (value !== null && value !== undefined && typeof value !== 'object') {
|
||
cell.value = String(value);
|
||
}
|
||
cell.numFmt = '@';
|
||
});
|
||
}
|
||
}
|
||
|
||
async addEmbeddedImages(workbook, worksheet, embeddedImages = []) {
|
||
for (const imageConfig of embeddedImages) {
|
||
if (!imageConfig) {
|
||
continue;
|
||
}
|
||
|
||
const row = Math.max(Number(imageConfig.row || 1), 1);
|
||
const col = Math.max(Number(imageConfig.col || imageConfig.column || 1), 1);
|
||
const width = Math.max(Number(imageConfig.width || 120), 40);
|
||
const height = Math.max(Number(imageConfig.height || 120), 40);
|
||
const cell = worksheet.getCell(row, col);
|
||
const fallbackText = imageConfig.fallbackText || imageConfig.url || '';
|
||
const resolvedImage = await this.resolveImageSource(imageConfig);
|
||
|
||
if (!resolvedImage || !resolvedImage.buffer || resolvedImage.buffer.length === 0) {
|
||
if (fallbackText) {
|
||
cell.value = fallbackText;
|
||
}
|
||
continue;
|
||
}
|
||
|
||
const imageId = workbook.addImage({
|
||
buffer: resolvedImage.buffer,
|
||
extension: resolvedImage.extension,
|
||
});
|
||
|
||
worksheet.addImage(imageId, {
|
||
tl: { col: col - 1 + 0.05, row: row - 1 + 0.05 },
|
||
ext: { width, height },
|
||
editAs: 'oneCell',
|
||
});
|
||
|
||
if (fallbackText) {
|
||
cell.value = fallbackText;
|
||
}
|
||
|
||
worksheet.getRow(row).height = Math.max(Number(worksheet.getRow(row).height || 20), Math.ceil(height * 0.75));
|
||
worksheet.getColumn(col).width = Math.max(Number(worksheet.getColumn(col).width || 10), Math.ceil(width / 7));
|
||
}
|
||
}
|
||
|
||
async generateBuffer(options = {}) {
|
||
const bookType = String(options.bookType || 'xlsx').replace('.', '').toLowerCase();
|
||
|
||
const shouldUseExcelJs = this.hasEmbeddedImages(options)
|
||
|| (Array.isArray(options.sheets)
|
||
&& options.sheets.some(sheet => sheet && (sheet.forceExcelJs || (Array.isArray(sheet.styledRows) && sheet.styledRows.length > 0))));
|
||
|
||
if (shouldUseExcelJs) {
|
||
if (bookType !== 'xlsx') {
|
||
throw new Error('带图片的 Excel 仅支持 xlsx 格式');
|
||
}
|
||
|
||
const workbook = await this.buildExcelJsWorkbook({
|
||
...options,
|
||
bookType,
|
||
});
|
||
const buffer = await workbook.xlsx.writeBuffer();
|
||
return Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
|
||
}
|
||
|
||
const workbook = this.buildWorkbook(options);
|
||
const buffer = XLSX.write(workbook, {
|
||
type: 'buffer',
|
||
bookType,
|
||
compression: true,
|
||
});
|
||
|
||
return Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
|
||
}
|
||
|
||
async writeFile(filePath, options = {}) {
|
||
if (!filePath) {
|
||
throw new Error('filePath 不能为空');
|
||
}
|
||
|
||
const resolvedFilePath = path.resolve(filePath);
|
||
const ext = path.extname(resolvedFilePath).replace('.', '').toLowerCase() || options.bookType || 'xlsx';
|
||
const buffer = await this.generateBuffer({
|
||
...options,
|
||
bookType: ext,
|
||
});
|
||
|
||
await fs.promises.mkdir(path.dirname(resolvedFilePath), { recursive: true });
|
||
await this.writeAtomicBufferFile(resolvedFilePath, buffer);
|
||
|
||
return {
|
||
filePath: resolvedFilePath,
|
||
size: buffer.length,
|
||
bookType: ext,
|
||
};
|
||
}
|
||
|
||
async writeAtomicBufferFile(targetPath, buffer) {
|
||
const directory = path.dirname(targetPath);
|
||
const tempPath = path.join(
|
||
directory,
|
||
`.${path.basename(targetPath)}.${process.pid}.${Date.now()}.${crypto.randomBytes(6).toString('hex')}.tmp`
|
||
);
|
||
|
||
await fs.promises.writeFile(tempPath, buffer);
|
||
|
||
try {
|
||
await fs.promises.rename(tempPath, targetPath);
|
||
} catch (error) {
|
||
if (error && (error.code === 'EEXIST' || error.code === 'EPERM' || error.code === 'EBUSY')) {
|
||
await fs.promises.rm(targetPath, { force: true });
|
||
await fs.promises.rename(tempPath, targetPath);
|
||
return;
|
||
}
|
||
|
||
throw error;
|
||
} finally {
|
||
await fs.promises.rm(tempPath, { force: true }).catch(() => {});
|
||
}
|
||
}
|
||
|
||
sanitizeFilename(filename = 'export.xlsx', fallbackExt = '.xlsx') {
|
||
const normalizedName = String(filename || '').trim() || `export${fallbackExt}`;
|
||
const rawExt = path.extname(normalizedName);
|
||
const ext = rawExt || fallbackExt;
|
||
const baseName = path.basename(normalizedName, rawExt);
|
||
const safeBaseName = String(baseName || 'export')
|
||
.replace(/[<>:"/\\|?*]/g, '_')
|
||
.replace(/\s+/g, '_')
|
||
.replace(/_+/g, '_')
|
||
.replace(/^_+|_+$/g, '')
|
||
.slice(0, 120) || 'export';
|
||
|
||
return `${safeBaseName}${ext}`;
|
||
}
|
||
|
||
buildUploadKey(filename, options = {}) {
|
||
if (options.key) {
|
||
return String(options.key)
|
||
.replace(/\\/g, '/')
|
||
.replace(/^\/+/, '')
|
||
.replace(/\/+/g, '/');
|
||
}
|
||
|
||
const now = new Date();
|
||
const dateFolder = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`;
|
||
const normalizedDir = String(options.dir || 'exports/excel')
|
||
.replace(/\\/g, '/')
|
||
.replace(/^\/+|\/+$/g, '');
|
||
|
||
return path.posix.join(normalizedDir, dateFolder, filename);
|
||
}
|
||
|
||
async generateAndUpload({
|
||
filename = 'export.xlsx',
|
||
dir = 'exports/excel',
|
||
key,
|
||
category = 'document',
|
||
saveRecord = false,
|
||
businessType,
|
||
businessId,
|
||
uploaderDept,
|
||
expireTime,
|
||
expireDays,
|
||
onProgress,
|
||
...options
|
||
} = {}) {
|
||
const uploadService = this.ctx && this.ctx.service && this.ctx.service.systems && this.ctx.service.systems.upload;
|
||
|
||
if (!uploadService || typeof uploadService.uploadFile !== 'function') {
|
||
throw new Error('上传服务不可用,无法上传 Excel 文件');
|
||
}
|
||
|
||
const bookType = String(options.bookType || path.extname(filename).replace('.', '') || 'xlsx').toLowerCase();
|
||
const safeFilename = this.sanitizeFilename(filename, `.${bookType}`);
|
||
const contentType = this.getContentType(bookType);
|
||
const buffer = await this.generateBuffer({
|
||
...options,
|
||
bookType,
|
||
});
|
||
const md5Hash = crypto.createHash('md5').update(buffer).digest('hex');
|
||
const sha1Hash = crypto.createHash('sha1').update(buffer).digest('hex');
|
||
const uploadKey = this.buildUploadKey(safeFilename, { dir, key });
|
||
const uploadStream = Readable.from(buffer);
|
||
|
||
uploadStream.filename = safeFilename;
|
||
uploadStream.mimeType = contentType;
|
||
uploadStream.fileSize = buffer.length;
|
||
|
||
const uploadResult = await uploadService.uploadFile(uploadStream, {
|
||
key: uploadKey,
|
||
category,
|
||
mimetype: 'xls,xlsx,csv',
|
||
onProgress,
|
||
});
|
||
|
||
let fileRecord = null;
|
||
if ((saveRecord || businessType || businessId) && typeof uploadService.uploadSuccessNotify === 'function') {
|
||
const recordResult = await uploadService.uploadSuccessNotify({
|
||
url: uploadResult.fileName,
|
||
fileKey: uploadResult.fileName,
|
||
fileName: safeFilename,
|
||
name: safeFilename,
|
||
fileSize: buffer.length,
|
||
size: buffer.length,
|
||
fileType: category,
|
||
mimeType: contentType,
|
||
type: category,
|
||
category,
|
||
businessType,
|
||
businessId,
|
||
uploaderDept,
|
||
expireTime,
|
||
expireDays,
|
||
md5Hash,
|
||
sha1Hash,
|
||
});
|
||
|
||
fileRecord = recordResult && (recordResult.data || recordResult);
|
||
}
|
||
|
||
return {
|
||
filename: safeFilename,
|
||
fileKey: uploadResult.fileName,
|
||
size: buffer.length,
|
||
contentType,
|
||
...uploadResult,
|
||
fileRecord,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 上传已经生成到本地临时目录的 Excel 文件(供流式生成场景复用上传链路)。
|
||
* 与 generateAndUpload 的区别:文件由调用方(如 ExcelJS.stream.WorkbookWriter)落盘,
|
||
* 这里只负责归一化文件名、构建上传 key、流式上传与可选的落库记录。
|
||
* @param {Object} options 参数
|
||
* @param {string} options.filePath 本地文件绝对路径
|
||
* @param {string} [options.filename] 原始文件名
|
||
* @param {string} [options.dir='exports/excel'] 上传目录
|
||
* @param {string} [options.key] 自定义上传 key(优先于 dir)
|
||
* @param {string} [options.category='document'] 文件分类
|
||
* @param {boolean} [options.saveRecord=false] 是否落库记录
|
||
* @param {string} [options.businessType] 业务类型(落库记录用)
|
||
* @param {string} [options.businessId] 业务ID(落库记录用)
|
||
* @param {string} [options.uploaderDept] 上传部门(落库记录用)
|
||
* @param {string|number} [options.expireTime] 过期时间(落库记录用)
|
||
* @param {number} [options.expireDays] 过期天数(落库记录用)
|
||
* @param {Function} [options.onProgress] 上传进度回调
|
||
* @return {Promise<Object>} 上传结果(含 filename/fileKey/size/contentType 与 uploadFile 返回值)
|
||
*/
|
||
async uploadExistingFile({
|
||
filePath,
|
||
filename = 'export.xlsx',
|
||
dir = 'exports/excel',
|
||
key,
|
||
category = 'document',
|
||
saveRecord = false,
|
||
businessType,
|
||
businessId,
|
||
uploaderDept,
|
||
expireTime,
|
||
expireDays,
|
||
onProgress,
|
||
...options
|
||
} = {}) {
|
||
const uploadService = this.ctx && this.ctx.service && this.ctx.service.systems && this.ctx.service.systems.upload;
|
||
|
||
if (!uploadService || typeof uploadService.uploadFile !== 'function') {
|
||
throw new Error('上传服务不可用,无法上传 Excel 文件');
|
||
}
|
||
|
||
if (typeof filePath !== 'string' || !filePath) {
|
||
throw new Error('filePath 不能为空');
|
||
}
|
||
|
||
const bookType = String(options.bookType || path.extname(filename).replace('.', '') || 'xlsx').toLowerCase();
|
||
const safeFilename = this.sanitizeFilename(filename, `.${bookType}`);
|
||
const contentType = this.getContentType(bookType);
|
||
const stat = await fs.promises.stat(filePath);
|
||
const uploadKey = this.buildUploadKey(safeFilename, { dir, key });
|
||
const uploadStream = fs.createReadStream(filePath);
|
||
|
||
uploadStream.filename = safeFilename;
|
||
uploadStream.mimeType = contentType;
|
||
uploadStream.fileSize = stat.size;
|
||
|
||
const uploadResult = await uploadService.uploadFile(uploadStream, {
|
||
key: uploadKey,
|
||
category,
|
||
mimetype: 'xls,xlsx,csv',
|
||
onProgress,
|
||
});
|
||
|
||
let fileRecord = null;
|
||
if ((saveRecord || businessType || businessId) && typeof uploadService.uploadSuccessNotify === 'function') {
|
||
const recordResult = await uploadService.uploadSuccessNotify({
|
||
url: uploadResult.fileName,
|
||
fileKey: uploadResult.fileName,
|
||
fileName: safeFilename,
|
||
name: safeFilename,
|
||
fileSize: stat.size,
|
||
size: stat.size,
|
||
fileType: category,
|
||
mimeType: contentType,
|
||
type: category,
|
||
category,
|
||
businessType,
|
||
businessId,
|
||
uploaderDept,
|
||
expireTime,
|
||
expireDays,
|
||
});
|
||
|
||
fileRecord = recordResult && (recordResult.data || recordResult);
|
||
}
|
||
|
||
return {
|
||
filename: safeFilename,
|
||
fileKey: uploadResult.fileName,
|
||
size: stat.size,
|
||
contentType,
|
||
...uploadResult,
|
||
fileRecord,
|
||
};
|
||
}
|
||
|
||
async sendFile({ ctx = this.ctx, filename = 'export.xlsx', ...options } = {}) {
|
||
if (!ctx) {
|
||
throw new Error('ctx 不存在,无法输出下载响应');
|
||
}
|
||
|
||
const ext = path.extname(filename).replace('.', '').toLowerCase() || options.bookType || 'xlsx';
|
||
const buffer = await this.generateBuffer({
|
||
...options,
|
||
bookType: ext,
|
||
});
|
||
|
||
ctx.set('Content-Type', this.getContentType(ext));
|
||
ctx.set('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`);
|
||
ctx.body = buffer;
|
||
|
||
return {
|
||
filename,
|
||
size: buffer.length,
|
||
contentType: this.getContentType(ext),
|
||
};
|
||
}
|
||
|
||
async parseBuffer(buffer, options = {}) {
|
||
if (!buffer) {
|
||
throw new Error('buffer 不能为空');
|
||
}
|
||
|
||
const workbook = XLSX.read(buffer, {
|
||
type: 'buffer',
|
||
raw: false,
|
||
cellDates: true,
|
||
...options.readOptions,
|
||
});
|
||
|
||
return this.workbookToJSON(workbook, options);
|
||
}
|
||
|
||
async parseFile(filePath, options = {}) {
|
||
if (!filePath) {
|
||
throw new Error('filePath 不能为空');
|
||
}
|
||
|
||
const resolvedFilePath = path.resolve(filePath);
|
||
const buffer = await fs.promises.readFile(resolvedFilePath);
|
||
const result = await this.parseBuffer(buffer, options);
|
||
|
||
return {
|
||
filePath: resolvedFilePath,
|
||
...result,
|
||
};
|
||
}
|
||
|
||
workbookToJSON(workbook, options = {}) {
|
||
const {
|
||
headerRow = 1,
|
||
defval = '',
|
||
blankrows = false,
|
||
includeRawRows = false,
|
||
sheetNames,
|
||
} = options;
|
||
|
||
const startIndex = Math.max(Number(headerRow) || 1, 1) - 1;
|
||
const selectedSheetNames = Array.isArray(sheetNames) && sheetNames.length > 0
|
||
? workbook.SheetNames.filter(name => sheetNames.includes(name))
|
||
: workbook.SheetNames;
|
||
|
||
const sheets = selectedSheetNames.map(name => {
|
||
const sheet = workbook.Sheets[name];
|
||
const rawRows = XLSX.utils.sheet_to_json(sheet, {
|
||
header: 1,
|
||
raw: false,
|
||
defval,
|
||
blankrows,
|
||
});
|
||
const headerValues = Array.isArray(rawRows[startIndex]) ? rawRows[startIndex] : [];
|
||
const headers = this.normalizeHeaders(headerValues);
|
||
const dataRows = rawRows.slice(startIndex + 1)
|
||
.filter(row => blankrows || row.some(cell => String(cell == null ? '' : cell).trim() !== ''));
|
||
const rows = dataRows.map(row => {
|
||
return headers.reduce((result, headerName, columnIndex) => {
|
||
result[headerName] = row[columnIndex] == null ? defval : row[columnIndex];
|
||
return result;
|
||
}, {});
|
||
});
|
||
|
||
const sheetResult = {
|
||
name,
|
||
headers,
|
||
rows,
|
||
rowCount: rows.length,
|
||
};
|
||
|
||
if (includeRawRows) {
|
||
sheetResult.rawRows = rawRows;
|
||
}
|
||
|
||
return sheetResult;
|
||
});
|
||
|
||
return {
|
||
sheetNames: selectedSheetNames,
|
||
sheets,
|
||
};
|
||
}
|
||
}
|
||
|
||
module.exports = ExcelService;
|