table导出excel(xlsx版本高于0.18)
1.npm安装依赖
npm install xlsx fs
yarn add xlsx
yarn add fs
表格添加属性:
标签添加ref属性,用于获取该元素el;也可添加id属性获取
<el-table :data="datas" style="width: 100%" id="exportTableRef"></el-table>
实现代码部分:
// 导入依赖 import * as XLSX from "xlsx/xlsx.mjs"; // 导出方法 exportBtn() { let table_elt = document.getElementById("exportTableRef") // Extract Data (create a workbook object from the table) let workbook = XLSX.utils.table_to_book(table_elt); // Process Data (add a new row) let ws = workbook.Sheets["Sheet1"]; XLSX.utils.sheet_add_aoa(ws, [], { origin: -1, }); // Package and Release Data (`writeFile` tries to write and save an XLSB file) XLSX.writeFile(workbook, "导出.xlsx"); }