el-table导出为excel表格
目录
1.下载依赖
2.引入插件
3.定义函数
4.设置table的id
1.下载依赖
npm intall --save xlsx
npm intall --save file-saver
2.引入插件
import FileSaver from "file-saver";
import XLSX from "xlsx";
3.定义函数
exportExcel() {let fix = document.querySelector(".el-table__fixed");let wb;if (fix) {//判断要导出的节点中是否有fixed的表格,如果有,转换excel时先将该dom移除,然后append回去 可以避免行重复wb = XLSX.utils.table_to_book(document.querySelector("#table").removeChild(fix));document.querySelector("#table").appendChild(fix);} else {wb = XLSX.utils.table_to_book(document.querySelector("#table"));}let wbout = XLSX.write(wb, {bookType: "xlsx",bookSST: true,type: "array",});try {//文件名可以自定义FileSaver.saveAs(new Blob([wbout], { type: "application/octet-stream" }),"仪表数据.xlsx");} catch (e) {if (typeof console !== "undefined") console.log(e, wbout);}return wbout;},
4.设置table的id
<el-tableref="exportTable":data="tableData"borderstyle="width: 100%"id="table">