745 lines
22 KiB
JavaScript
745 lines
22 KiB
JavaScript
'use strict';
|
|
|
|
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 = [],
|
|
} = 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;
|
|
}
|
|
|
|
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 = [],
|
|
} = 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(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);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
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 fs.promises.writeFile(resolvedFilePath, buffer);
|
|
|
|
return {
|
|
filePath: resolvedFilePath,
|
|
size: buffer.length,
|
|
bookType: ext,
|
|
};
|
|
}
|
|
|
|
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 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,
|
|
});
|
|
|
|
fileRecord = recordResult && (recordResult.data || recordResult);
|
|
}
|
|
|
|
return {
|
|
filename: safeFilename,
|
|
fileKey: uploadResult.fileName,
|
|
size: buffer.length,
|
|
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;
|