先安装
excel.js
yarn add exceljs
在代码中引用 ```javascript const workbook = new ExcelJS.Workbook();
// 生成sheet,冻结第一列、第一行 const sheet = workbook.addWorksheet(“Sheet 1”, {views: [{state: “frozen”, xSplit: 1, ySplit: 1}]})
3. 准备数据等 (建议先大致建立一个想要导出的excel,然后对比设置会比较方便)
![image.png](https://cdn.nlark.com/yuque/0/2021/png/886344/1637649748396-8814a59b-0fee-4721-88f4-0248b51670f0.png#clientId=ua9f12f21-a1a6-4&from=paste&height=132&id=u1d8ec795&margin=%5Bobject%20Object%5D&name=image.png&originHeight=132&originWidth=458&originalType=binary&ratio=1&size=10797&status=done&style=none&taskId=u5f1d7bb7-1619-4d5b-895b-695d3138cce&width=458)
```javascript
const data = []
// 创建表头 ,需要合并的单元格使用 null 代替
data.push(["成绩统计表", null, null, null, null, null]);
data.push(["序号", "姓名", "班级", "性别", "成绩", null]);
data.push([null, null, null, null, "语文", "数学"]);
//根据 null 合并单元格 (好像有点小问题,需要再调试调试)
data.forEach((i, index) => {
sheet.addRow(i)
let isNull = false
let mergeObj = {}
if (index < 2) {
i.forEach((j, k) => {
if (j === null && !isNull && Object.keys(mergeObj).length === 0) {
mergeObj.top = index + 1
mergeObj.left = k
isNull = true
} else if ((j && isNull) || (j === null && k === i.length - 1)) {
isNull = false
mergeObj.bottom = index + 1
mergeObj.right = k
sheet.mergeCells(mergeObj.top, mergeObj.left, mergeObj.bottom, mergeObj.right)
mergeObj = {}
}
}
)
} else if (index === 2) {
i.forEach((j, k) => {
if (j === null) {
sheet.mergeCells(`${abc[k]}${index}:${abc[k]}${index + 1}`)
}
})
}
}
)
//根据dataSource,往sheet中存放数据
const dataSource = [
{name: "张三", class: "一班", gender: "男", ChineseGrade: "78", MathGrade: "89"},
{name: "李四", class: "一班", gender: "男", ChineseGrade: "89", MathGrade: "78"},
];
dataSource.forEach((i, index) => {
sheet.addRow([
index+1,
i.name,
i.class,
i.gender,
i.ChineseGrade,
i.MathGrade
]);
});
修改表格样式等 ```javascript // 对每行、每行中的单元格设置行高、字体、边框等 sheet.eachRow({includeEmpty: true}, (row, rowNumber) => { //设置行高,顺便设置第二、第三行表头的字体等 if (rowNumber === 1) {
row.height = 50
row.eachCell((cell) => {
cell.font = {
name: '宋体',
size: 36,
bold: true
}
})
} else if (rowNumber === 2 || rowNumber === 3) {
row.height = 20
row.eachCell((cell, cellNumber) => {
if ((cellNumber > 9 && cellNumber < 12) || (cellNumber > 12 && cellNumber < 16) || (cellNumber > 20 && cellNumber < 26)) {
cell.font = {
name: '宋体',
size: rowNumber === 2 ? 16 : 14,
bold: rowNumber === 2
}
} else {
cell.font = {
name: '宋体',
size: 16,
bold: true
}
}
})
} else {
//通用行高
row.height = 25
}
//遍历所有包括空的单元格,设置样式等 row.eachCell({includeEmpty: true}, (cell) => {
cell.alignment = {vertical: 'middle', horizontal: 'center', wrapText: true} //居中对齐,自动换行
cell.border = {
top: {style: 'thin'},
left: {style: 'thin'},
bottom: {style: 'thin'},
right: {style: 'thin'}
}
if (rowNumber > 3) { // 对第三行以上的设置字题
cell.font = {
name: '宋体',
size: 14
}
}
}) })
//设置每一列的列宽 sheet.columns = [ {width:8}, //序号 {width:10}, //姓名 {width:10}, //班级 {width:8}, //性别 {width:10}, //语文 {width:10}, //数学 ]
5. 导出文件
```javascript
// 导出文件
const writeFile = (fileName: string, content: Buffer) => {
let a = document.createElement("a");
let blob = new Blob([content], {type: "text/plain"});
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.click();
};
workbook.xlsx.writeBuffer().then((buffer) => {
// @ts-ignore
return writeFile(`成绩统计表.xlsx`, buffer);
});