安装插件
file-saver 和 xlsx
组件中导入
import FileSaver from 'file-saver' import XLSX from 'xlsx'
写方法
outputFile() { var workbook = XLSX.utils.book_new(); /* convert table 'table1' to worksheet named "Sheet1" */ //对应要导出的表格id var ws1 = XLSX.utils.table_to_sheet(document.querySelector('#Table1')); 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; }
标签绑定方法
<el-button @click="outputFile">默认按钮</el-button>