import ExcelJS from 'exceljs'; /** * Generates an XLSX file from the given data. * @param {Array} data - The data to convert to XLSX. * @param {string} filePath - The path to save the XLSX file. */ async function generateXlsx(data, filePath) { if (data.length === 0) { console.log(`没有数据可生成 XLSX 文件 (${filePath})。`); return; } const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('Data'); const headers = Object.keys(data[0]); worksheet.columns = headers.map(key => ({ header: key, key: key, width: key.includes('地址') || key.includes('链接') ? 40 : 20 })); worksheet.addRows(data); worksheet.getRow(1).eachCell(cell => { cell.font = { bold: true }; cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFDDDDDD' } }; cell.alignment = { vertical: 'middle', horizontal: 'center' }; }); worksheet.autoFilter = { from: 'A1', to: { row: 1, column: headers.length } }; await workbook.xlsx.writeFile(filePath); console.log(`已生成 ${filePath} 文件。`); }