1.安装依赖
npm install xlsx file-saver --save
2.组件中导入
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
tips:这里注意xlsx版本,如果报xlsx找不到utils,需要变更引入方式
import * as XLSX from 'xlsx'
3.dom
<el-table id='exportExecl'>
<el-button @click="exportExecl()">exportExecl</el-button>
4.js
const exportExcel = () => {const useXlsx = XLSX.utils.table_to_book(document.getElementById('exportExcel '))const xlsxOut = XLSX.write(useXlsx, {bookType: "xlsx",bookSST: true,type: "array"});try {FileSaver.saveAs(new Blob([xlsxOut ], {type: "application/octet-stream"}),"exportExcel .xlsx");} catch (e) {console.error(e);}
}