前言
在开发过程中,我们经常需要将表格数据导出为 Excel 文件。大多数情况下,由后端处理即可,但是当数据量不大、需要快速响应用户操作、或者数据已经在前端进行处理和展示时,前端该如何实现呢。本文将介绍两种方法:一种是使用 xlsx.js
进行简单导出,另一种是使用 xlsx-style-medalsoft
进行样式化导出,包括多 sheet 支持。
方式一:简单导出
首先,我们使用 xlsx.js
库实现基本的表格导出功能。这种方法不包含样式,适用于快速导出数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 | import * as XLSX from 'xlsx' ; // 创建一个新的工作簿 const book = XLSX.utils.book_new(); // 将 HTML 表格转换为工作表 const sheet = XLSX.utils.table_to_sheet( this .$refs.exportTableRef.$el); // 将工作表添加到工作簿 XLSX.utils.book_append_sheet(book, sheet, "Sheet1" ); // 导出工作簿为 Excel 文件 XLSX.writeFile(book, "xxx统计.xlsx" ); |
方式二:多 sheet 导出,带样式
对于更复杂的需求,如导出多个工作表或添加样式,我们可以使用 xlsx-style-medalsoft
库。
安装依赖
首先,安装必要的依赖:
这里注意版本号,有些版本可能会出现问题,我本地目前版本如下
1 2 3 | "file-saver" : "^2.0.5" , "xlsx" : "^0.16.9" , "xlsx-style-medalsoft" : "^0.8.13" |
封装导出功能
接下来,我们封装一个函数,用于导出带样式的 Excel 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import FileSaver from 'file-saver' ; import * as XLSX from 'xlsx' ; import XLSXStyle from 'xlsx-style-medalsoft' ; const OMS = {}; // 导出 Excel 文件,支持多 sheet 和样式 OMS.downLoadXlsx = ({ sheets = [], name = '文件' }) => { const workbook = XLSX.utils.book_new(); sheets.forEach(sheetData => { const { dom, sheetName, columnWidth = [], raw } = sheetData; const table = dom; // 移除固定列,避免重复内容 [ 'el-table__fixed' , 'el-table__fixed-right' ].forEach(className => { const fixedTable = table.querySelector(`.${className}`); if (fixedTable) table.removeChild(fixedTable); }); // 转换表格为工作表 const sheet = XLSX.utils.table_to_sheet(table, { raw }); // 删除空行 Object.keys(sheet).forEach(key => { if (!key.startsWith( '!' ) && sheet[key].v === '' ) delete sheet[key]; }); // 设置列宽度 if (columnWidth.length > 0) { columnWidth.forEach((width, index) => { sheet[ '!cols' ][index] = { wch: width }; }); } else { // 默认列宽 for ( let i = 0; i { if (!key.startsWith( '!' )) { sheet[key].s = { font: { sz: 11, bold: false , name: '宋体' , color: { rgb: '000000' } }, alignment: { horizontal: 'center' , vertical: 'center' , wrapText: false }, border: {} }; } }); // 合并单元格 const range = sheet[ '!merges' ]; if (range) { range.forEach(item => { const startCol = item.s.c, endCol = item.e.c; const startRow = item.s.r, endRow = item.e.r; const firstCell = sheet[XLSX.utils.encode_cell({ r: startRow, c: startCol })]; for ( let row = startRow; row |
使用方法
最后,我们在需要使用的地方引入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import OMS from "@/utils/exportToExcel" ; OMS.downLoadXlsx({ sheets: [ { dom: this .$refs.rowTableRef.$el, sheetName: this .rowData[0].hospName, columnWidth: [20, 15, 15, 15, 15, 15, 15, 15] // 可选,设置列宽度 }, { dom: this .$refs.table.$el, sheetName: "明细" , raw: true , columnWidth: [10, 15, 12, 15, 15, 15, 15, 15] // 可选,设置列宽度 } // 可添加更多工作表 ], name: "报告" }); |
通过这种方式,我们就实现了前端导出带样式的 Excel 文件,满足多 sheet,带样式的业务需求。
但是需要注意的是如果数据量不大、需要快速响应用户操作、或者数据已经在前端进行处理和展示,前端来做导出是可以的。而如果数据量很大、需要复杂的数据处理、或者需要保证数据的安全性和一致性,后端导出可能更合适。
以上就是Vue导出el-table表格为Excel文件的两种方式的详细内容,更多关于Vue导出el-table为Excel的资料请关注IT俱乐部其它相关文章!