当前位置:   article > 正文

前端使用exceljs实现Excel文件导出

exceljs
1、安装依赖
npm install file-saver exceljs
2、导出excel文件案例
  1. //引入依赖
  2. const ExcelJS = require("exceljs");
  3. import { saveAs } from "file-saver";
  4. export default {
  5. data(){
  6. return {
  7. headerRow:["年度", "班级", "排名",'学号','姓名','科目一','科目二','科目三'],
  8. titleRow:[
  9. { header: "年度", key: "year", width: 10 },
  10. { header: "班级", key: "className", width: 30 },
  11. { header: "排名", key: "sort", width: 30 },
  12. { header: "学号", key: "studentNumber", width: 30 },
  13. { header: "姓名", key: "name", width: 30 },
  14. ],
  15. tableLafterData:[
  16. { year: "2023", className: "2301", sort: 1 ,studentNumber:'20231301101',name:''},
  17. { year: "2023", className: "2301", sort: 2 ,studentNumber:'20231301102',name:''},
  18. { year: "2023", className: "2301", sort: 3,studentNumber:'20231301103',name:''},
  19. { year: "2023", className: "2301", sort: 4 ,studentNumber:'20231301104',name:''},
  20. { year: "2023", className: "2301", sort: 5 ,studentNumber:'20231301105',name:''},
  21. ]
  22. }
  23. },
  24. methods:{
  25. exportToExcel() {
  26. // 创建工作簿
  27. const _workbook = new ExcelJS.Workbook();
  28. // 添加工作表
  29. const _sheet1 = _workbook.addWorksheet("sheet1");
  30. // 设置表格内容
  31. const _titleCell0 = _sheet1.getCell("A1");
  32. const _titleCell = _sheet1.getCell("A2");
  33. _titleCell0.value = "附件1";
  34. _titleCell.value = "2023年xxx中学高一年级期中考试成绩汇总表";
  35. // 按左,右合并
  36. _sheet1.mergeCells("A1:H1");
  37. _sheet1.mergeCells("A2:H2");
  38. //第3行插入一行设置表头
  39. _sheet1.spliceRows(3, 0, []);
  40. this.headerRow.forEach((header, index) => {
  41. const cell = _sheet1.getCell(3, index + 1);
  42. cell.value = header;
  43. cell.font = { bold: true };
  44. // 设置表头单元格的背景颜色为蓝色
  45. cell.fill = {
  46. type: "pattern",
  47. pattern: "solid",
  48. fgColor: { argb: "00b0f0" }, // 设置为蓝色
  49. };
  50. cell.border = {
  51. top: {
  52. style: "medium",
  53. color: {
  54. argb: "FFEBEEF5",
  55. },
  56. },
  57. left: {
  58. style: "medium",
  59. color: {
  60. argb: "FFEBEEF5",
  61. },
  62. },
  63. bottom: {
  64. style: "medium",
  65. color: {
  66. argb: "FFEBEEF5",
  67. },
  68. },
  69. right: {
  70. style: "medium",
  71. color: {
  72. argb: "FFEBEEF5",
  73. },
  74. },
  75. };
  76. // 设置超出列宽时的换行显示
  77. cell.alignment = {
  78. wrapText: true,
  79. vertical: "middle",
  80. horizontal: "center",
  81. };
  82. });
  83. // 设置第3行的行高为
  84. _sheet1.getRow(3).height = 63;
  85. // 添加数据到工作表
  86. this.tableLafterData.forEach((item, rowIndex) => {
  87. const rowValues = this.titleRow.map((config) => item[config.key]);
  88. const row = _sheet1.addRow(rowValues, rowIndex + 3);
  89. row.eachCell((cell) => {
  90. cell.alignment = { vertical: "middle", horizontal: "center" };
  91. cell.font = { size: 9 };
  92. cell.alignment = {
  93. wrapText: true,
  94. vertical: "middle",
  95. horizontal: "center",
  96. };
  97. });
  98. });
  99. // 数据相同时,需要进行纵向合并的列
  100. const mergeColumns = []
  101. this.titleRow.forEach((column, columnIndex) => {
  102. // 设置列宽度
  103. _sheet1.getColumn(columnIndex + 1).width = column.width;
  104. if (mergeColumns.includes(column.key)) {
  105. let mergeStartRow = 3; // 开始合并的行
  106. let mergeEndRow = mergeStartRow;
  107. let previousValue = _sheet1.getCell(
  108. mergeStartRow,
  109. columnIndex + 1
  110. ).value;
  111. for (
  112. let rowIndex = mergeStartRow + 1;
  113. rowIndex <= _sheet1.rowCount;
  114. rowIndex++
  115. ) {
  116. const currentValue = _sheet1.getCell(
  117. rowIndex,
  118. columnIndex + 1
  119. ).value;
  120. if (currentValue !== previousValue) {
  121. // 字段值发生变化,进行合并
  122. if (mergeEndRow > mergeStartRow) {
  123. _sheet1.mergeCells(
  124. mergeStartRow,
  125. columnIndex + 1,
  126. mergeEndRow,
  127. columnIndex + 1
  128. );
  129. }
  130. mergeStartRow = rowIndex;
  131. mergeEndRow = rowIndex;
  132. previousValue = currentValue;
  133. } else {
  134. // 字段值相同,继续向下合并
  135. mergeEndRow = rowIndex;
  136. }
  137. }
  138. // 处理最后一个字段值的合并
  139. if (mergeEndRow > mergeStartRow) {
  140. _sheet1.mergeCells(
  141. mergeStartRow,
  142. columnIndex + 1,
  143. mergeEndRow,
  144. columnIndex + 1
  145. );
  146. }
  147. }
  148. });
  149. // 设置第一行的高度
  150. _titleCell0.height = 18;
  151. _titleCell.height = 36;
  152. // 设置第一行的字体样式
  153. _titleCell.font = {
  154. name: "方正小标宋_GBK",
  155. bold: true,
  156. size: 14,
  157. };
  158. _titleCell0.font = {
  159. name: "黑体",
  160. bold: true,
  161. size: 12,
  162. color: {
  163. argb: "FF999999",
  164. },
  165. };
  166. // 设置第一行的对齐方式(水平垂直)
  167. _titleCell.alignment = {
  168. vertical: "middle",
  169. horizontal: "center",
  170. };
  171. // 设置边框线的样式
  172. _titleCell.border = {
  173. top: {
  174. style: "medium",
  175. color: {
  176. argb: "FFEBEEF5",
  177. },
  178. },
  179. left: {
  180. style: "medium",
  181. color: {
  182. argb: "FFEBEEF5",
  183. },
  184. },
  185. bottom: {
  186. style: "medium",
  187. color: {
  188. argb: "FFEBEEF5",
  189. },
  190. },
  191. right: {
  192. style: "medium",
  193. color: {
  194. argb: "FFEBEEF5",
  195. },
  196. },
  197. };
  198. /// 设置单元的样式
  199. _titleCell.fill = {
  200. type: "pattern",
  201. pattern: "solid",
  202. fgColor: {
  203. argb: "FFF5F7FA",
  204. },
  205. };
  206. // 导出表格
  207. _workbook.xlsx.writeBuffer().then((buffer) => {
  208. let _file = new Blob([buffer], {
  209. type: "application/octet-stream",
  210. });
  211. saveAs(_file, "data.xlsx");
  212. });
  213. }
  214. }
  215. }
3、官方文档
https://github.com/exceljs/exceljs/blob/master/README_zh.md

4、效果图展示

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/668975
推荐阅读
相关标签
  

闽ICP备14008679号