第一步安装:
npm install xlsx file-saver
第二步导入:
import * as XLSX from 'xlsx'
import saveAs from 'file-saver'
第三步写函数:
function exportExcel(){
const workbook = XLSX.utils.book_new()
const worksheet = XLSX.utils.json_to_sheet(tableData)
XLSX.utils.book_append_sheet(workbook,worksheet,'Sheet1')
const excelData = XLSX.write(workbook,{bookType:'xlsx',type:'array'})
const blob = new Blob([excelData],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
saveAs(blob,'table_data.xlsx')
}
其中tableData就是el-table中的数据源