当前位置:   article > 正文

[最简单]exceljs 纯前端导出Excel,带样式,保护锁定_js excel加密导出

js excel加密导出
  1. /**
  2. * exceljs方法封装处理
  3. * Copyright (c) 2024 sanghx
  4. */
  5. import ExcelJS from "exceljs";
  6. /**
  7. * 导出Excel表格
  8. * @param {Array} columns 表头 [{title:"姓名",dataIndex:"name"}]
  9. * @param {Array} tableData 表数据 [{name:'张三'}]
  10. * @param {String} name 表名称
  11. * @param {Array} colWidth 列宽
  12. */
  13. const exportExcel = async ({
  14. tableData = [],
  15. columns = [],
  16. name = "",
  17. colWidth = [],
  18. }) => {
  19. const workbook = new ExcelJS.Workbook();
  20. const worksheet = workbook.addWorksheet("Sheet");
  21. worksheet.getRow(1).getCell(1).value = name
  22. worksheet.mergeCells(1, 1, 1, columns.length)
  23. worksheet.getRow(1).getCell(1).alignment = {
  24. horizontal: "center",// 水平居中
  25. vertical: 'middle', // 垂直居中
  26. }
  27. worksheet.getRow(1).getCell(1).font = { bold: true,size: 14 };
  28. //保护工作表
  29. worksheet.protect("111", {
  30. objects: true,
  31. scenarios: false,
  32. selectLockedCells: true,
  33. selectUnlockedCells: true,
  34. formatCells: true,
  35. formatColumns: true,
  36. formatRows: true,
  37. insertColumns: false,
  38. insertRows: false,
  39. insertHyperlinks: false,
  40. deleteColumns: false,
  41. deleteRows: false,
  42. sort: false,
  43. autoFilter: false,
  44. pivotTables: false,
  45. });
  46. // 表头
  47. const headerRow = worksheet.addRow(columns.map((column) => column.title));
  48. // 表头样式
  49. headerRow.eachCell((cell) => {
  50. cell.font = { bold: true,size: 12 };
  51. cell.alignment = {
  52. horizontal: "center",// 水平居中
  53. vertical: 'middle', // 垂直居中
  54. };
  55. });
  56. // 数据
  57. tableData.forEach((rowData) => {
  58. worksheet.addRow(columns.map((column) => rowData[column.dataIndex] || ""));
  59. });
  60. worksheet.getRow(1).height = 30;
  61. worksheet.getRow(2).height = 30;
  62. // 添加边框
  63. for (let num = 0; num < worksheet.lastRow._number; num++) {
  64. // 循环出每一行
  65. for (let index = 0; index < columns.length; index++) {
  66. // 循环出每一个单元格
  67. worksheet.getRow(num + 1).getCell(index + 1).border = {
  68. // 为单元格添加边框
  69. top: { style: "thin" },
  70. left: { style: "thin" },
  71. bottom: { style: "thin" },
  72. right: { style: "thin" },
  73. };
  74. }
  75. }
  76. // 列宽
  77. if (colWidth.length) {
  78. colWidth.forEach((width, index) => {
  79. worksheet.getColumn(index + 1).width = width || 20;
  80. });
  81. } else {
  82. for (let index = 0; index < columns.length; index++) {
  83. worksheet.getColumn(index + 1).width = columns[index].title.length * 2 + 10;
  84. }
  85. }
  86. // 生成Excel文件
  87. const buffer = await workbook.xlsx.writeBuffer();
  88. saveExcelFile(buffer, name);
  89. };
  90. // 下载Excel文件
  91. const saveExcelFile = (buffer, name) => {
  92. const blob = new Blob([buffer], {
  93. type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  94. });
  95. const url = window.URL.createObjectURL(blob);
  96. const link = document.createElement("a");
  97. link.href = url;
  98. link.setAttribute("download", `${name}.xlsx`);
  99. document.body.appendChild(link);
  100. link.click();
  101. document.body.removeChild(link);
  102. window.URL.revokeObjectURL(url);
  103. };
  104. export default exportExcel;

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号