1. 先安装excel.js

      1. yarn add exceljs
    2. 在代码中引用 ```javascript const workbook = new ExcelJS.Workbook();

    // 生成sheet,冻结第一列、第一行 const sheet = workbook.addWorksheet(“Sheet 1”, {views: [{state: “frozen”, xSplit: 1, ySplit: 1}]})

    1. 3. 准备数据等 (建议先大致建立一个想要导出的excel,然后对比设置会比较方便)
    2. ![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)
    3. ```javascript
    4. const data = []
    5. // 创建表头 ,需要合并的单元格使用 null 代替
    6. data.push(["成绩统计表", null, null, null, null, null]);
    7. data.push(["序号", "姓名", "班级", "性别", "成绩", null]);
    8. data.push([null, null, null, null, "语文", "数学"]);
    9. //根据 null 合并单元格 (好像有点小问题,需要再调试调试)
    10. data.forEach((i, index) => {
    11. sheet.addRow(i)
    12. let isNull = false
    13. let mergeObj = {}
    14. if (index < 2) {
    15. i.forEach((j, k) => {
    16. if (j === null && !isNull && Object.keys(mergeObj).length === 0) {
    17. mergeObj.top = index + 1
    18. mergeObj.left = k
    19. isNull = true
    20. } else if ((j && isNull) || (j === null && k === i.length - 1)) {
    21. isNull = false
    22. mergeObj.bottom = index + 1
    23. mergeObj.right = k
    24. sheet.mergeCells(mergeObj.top, mergeObj.left, mergeObj.bottom, mergeObj.right)
    25. mergeObj = {}
    26. }
    27. }
    28. )
    29. } else if (index === 2) {
    30. i.forEach((j, k) => {
    31. if (j === null) {
    32. sheet.mergeCells(`${abc[k]}${index}:${abc[k]}${index + 1}`)
    33. }
    34. })
    35. }
    36. }
    37. )
    38. //根据dataSource,往sheet中存放数据
    39. const dataSource = [
    40. {name: "张三", class: "一班", gender: "男", ChineseGrade: "78", MathGrade: "89"},
    41. {name: "李四", class: "一班", gender: "男", ChineseGrade: "89", MathGrade: "78"},
    42. ];
    43. dataSource.forEach((i, index) => {
    44. sheet.addRow([
    45. index+1,
    46. i.name,
    47. i.class,
    48. i.gender,
    49. i.ChineseGrade,
    50. i.MathGrade
    51. ]);
    52. });
    1. 修改表格样式等 ```javascript // 对每行、每行中的单元格设置行高、字体、边框等 sheet.eachRow({includeEmpty: true}, (row, rowNumber) => { //设置行高,顺便设置第二、第三行表头的字体等 if (rowNumber === 1) {

      1. row.height = 50
      2. row.eachCell((cell) => {
      3. cell.font = {
      4. name: '宋体',
      5. size: 36,
      6. bold: true
      7. }
      8. })

      } else if (rowNumber === 2 || rowNumber === 3) {

      1. row.height = 20
      2. row.eachCell((cell, cellNumber) => {
      3. if ((cellNumber > 9 && cellNumber < 12) || (cellNumber > 12 && cellNumber < 16) || (cellNumber > 20 && cellNumber < 26)) {
      4. cell.font = {
      5. name: '宋体',
      6. size: rowNumber === 2 ? 16 : 14,
      7. bold: rowNumber === 2
      8. }
      9. } else {
      10. cell.font = {
      11. name: '宋体',
      12. size: 16,
      13. bold: true
      14. }
      15. }
      16. })

      } else {

      1. //通用行高
      2. row.height = 25

      }

      //遍历所有包括空的单元格,设置样式等 row.eachCell({includeEmpty: true}, (cell) => {

      1. cell.alignment = {vertical: 'middle', horizontal: 'center', wrapText: true} //居中对齐,自动换行
      2. cell.border = {
      3. top: {style: 'thin'},
      4. left: {style: 'thin'},
      5. bottom: {style: 'thin'},
      6. right: {style: 'thin'}
      7. }
      8. if (rowNumber > 3) { // 对第三行以上的设置字题
      9. cell.font = {
      10. name: '宋体',
      11. size: 14
      12. }
      13. }

      }) })

    //设置每一列的列宽 sheet.columns = [ {width:8}, //序号 {width:10}, //姓名 {width:10}, //班级 {width:8}, //性别 {width:10}, //语文 {width:10}, //数学 ]

    1. 5. 导出文件
    2. ```javascript
    3. // 导出文件
    4. const writeFile = (fileName: string, content: Buffer) => {
    5. let a = document.createElement("a");
    6. let blob = new Blob([content], {type: "text/plain"});
    7. a.download = fileName;
    8. a.href = URL.createObjectURL(blob);
    9. a.click();
    10. };
    11. workbook.xlsx.writeBuffer().then((buffer) => {
    12. // @ts-ignore
    13. return writeFile(`成绩统计表.xlsx`, buffer);
    14. });