generate.excel.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import ExcelJS from "exceljs";
  2. import * as fs from "node:fs";
  3. import * as path from "node:path";
  4. // 创建工作簿和工作表
  5. const workbook = new ExcelJS.Workbook();
  6. /**
  7. * 想workbook添加worksheet
  8. * @param name 区域名
  9. * @param data 区域物资数据
  10. */
  11. function addWorkSheet(name, data) {
  12. const worksheet = workbook.addWorksheet(name);
  13. // 确保 Excel 中显示网格线
  14. worksheet.views = [{ showGridLines: true }];
  15. // 解析数据并逐行写入
  16. data.forEach(row => worksheet.addRow(row));
  17. //设置表头样式(深蓝底色、白字、加粗、居中)
  18. const headerRow = worksheet.getRow(1);
  19. headerRow.height = 28;
  20. headerRow.eachCell((cell) => {
  21. cell.border = {
  22. top: { style: "thin", color: { argb: "333333" } },
  23. left: { style: "thin", color: { argb: "333333" } },
  24. bottom: { style: "thin", color: { argb: "333333" } },
  25. right: { style: "thin", color: { argb: "333333" } }
  26. };
  27. cell.font = {
  28. name: "微软雅黑",
  29. size: 12,
  30. bold: true,
  31. color: { argb: "000000" }
  32. };
  33. cell.alignment = {
  34. horizontal: "center",
  35. vertical: "middle"
  36. };
  37. });
  38. // 设置数据行样式(细边框、对齐方式、行高)
  39. worksheet.eachRow((row, rowNumber) => {
  40. if(rowNumber > 1) {
  41. row.height = 20;
  42. row.eachCell((cell, colNumber) => {
  43. cell.font = {
  44. name: "微软雅黑",
  45. size: 12,
  46. color: { argb: "000000" }
  47. };
  48. cell.border = {
  49. top: { style: "thin", color: { argb: "333333" } },
  50. left: { style: "thin", color: { argb: "333333" } },
  51. bottom: { style: "thin", color: { argb: "333333" } },
  52. right: { style: "thin", color: { argb: "333333" } }
  53. };
  54. cell.alignment = {
  55. horizontal: [6, 7].includes(colNumber) ? "center" : "left",
  56. vertical: "middle"
  57. };
  58. });
  59. }
  60. });
  61. //自动根据内容调整列宽
  62. worksheet.columns.forEach(column => {
  63. let maxLength = 0;
  64. column.eachCell({ includeEmpty: true }, cell => {
  65. const columnLength = cell.value ? cell.value.toString().length : 0;
  66. if(columnLength > maxLength) {
  67. maxLength = columnLength;
  68. }
  69. });
  70. column.width = Math.max(maxLength + 4, 20);
  71. });
  72. }
  73. // 读取数据转成worksheet并添加到workbook
  74. const pathDataOrigin = "data-origin";
  75. const filenames = fs.readdirSync(path.resolve(pathDataOrigin));
  76. for (let i = 0; i < filenames.length; i++) {
  77. const filename = filenames[i];
  78. if(!filename.endsWith(".json")) continue;
  79. const textDataOrigin = fs.readFileSync(path.resolve(pathDataOrigin, filename)).toString();
  80. const areaGlobal = filename.replace("-物资表.json", "");
  81. const dataOrigin = JSON.parse(textDataOrigin);
  82. addWorkSheet(areaGlobal, dataOrigin);
  83. }
  84. // 保存文件
  85. await workbook.xlsx.writeFile(path.resolve("data-output/物资表.xlsx"));