npm 引入模块
npm i exceljs
直接上代码
const readFile = (buffer => {
let file = new FileReader();
file.readAsArrayBuffer(buffer);
file.onload = (e) => {
let a = document.createElement('a');
a.href = e.target.result;
a.download = 'test.xlsx';
document.append(a);
a.click();
}
})
// name 文件名称
// t 合并单元格
// tableData 表格数据
// title 头部标题
// widths 单元格宽度
export async function portTable(name, t, tableData, title, widths) {
const workbook = new ExcelJs.Workbook();
const worksheet = workbook.addWorksheet(name);
worksheet.mergeCells(t); // 合并单元格
worksheet.getRow(1).getCell(1).alignment = { // 找到第一行的第一个单元格
vertical: 'middle', horizontal: 'center' // 垂直居中,水平居中
}
worksheet.getCell("A1").fill={ // getCell("A1") == getRow(1).getCell(1)
type: 'pattern',
pattern:'solid',
fgColor:{ argb: 'FF0000FF'} // 设置单元格背景色
}
const handleData = tableData;
const columns = title;
worksheet.addTable({
name: name,
ref: 'A2', // 从A2单元格添加表格
style: {
theme: '',
showRowStripes: true
},
columns: columns, // 列
rows: handleData // 行
});
const width = widths;
columns.forEach((r, i) => { // 控制列的宽度,以及对齐方式
worksheet.getColumn(i + 1).width = width[i];
worksheet.getColumn(i + 1).alignment = { vertical: 'middle', horizontal: 'center' }
})
// 添加边框
for (let num = 0; num < worksheet.lastRow._number; num++) { // 循环出每一行
for (let index = 0; index < columns.length; index++) { // 循环出每一个单元格
worksheet.getRow(num+1).getCell(index+1).border={ // 为单元格添加边框
top: {style:'thin'},
left: {style:'thin'},
bottom: {style:'thin'},
right: {style:'thin'}
}
}
}
// console.log(worksheet.lastRow._number);
const buffer = await workbook.xlsx.writeBuffer();
return readFile (new Blob([buffer]), name + '.xlsx')
}