element-ui表格导出(多个表格导出多个sheet)
第一步:安装依赖
npm install xlsx file-saver --save
第二步:在组件中导入
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
第三步:1、给对应表格添加id 2、methods中写入方法
outputFile() {
var workbook = XLSX.utils.book_new();
/* convert table 'table1' to worksheet named "Sheet1" */
var ws1 = XLSX.utils.table_to_sheet(document.querySelector('#Table1'));//对应要导出的表格id
XLSX.utils.book_append_sheet(workbook, ws1, "sheet1");
/* convert table 'table2' to worksheet named "Sheet2" */
var ws2 = XLSX.utils.table_to_sheet(document.querySelector('#Table2'));
XLSX.utils.book_append_sheet(workbook, ws2, "sheet2");
/* get binary string as output */
var wbOut = XLSX.write(workbook, {
bookType: "xlsx",
bookSST: true,
type: "array"
});
try {
FileSaver.saveAs(
new Blob([wbOut], { type: "application/octet-stream" }),
"demo.xlsx"
);
} catch (e) {
if (typeof console !== "undefined") console.log(e, wbOut);
}
return wbOut;
}
第四步:给按钮绑定方法outputFile