npm安裝
npm install file-saver --save
npm install xlsx -s
使用
//html
<el-button type="primary" size="mini" @click="exportExcel">導(dǎo)出表格</el-button>
import * as XLSX from 'xlsx'
import { saveAs } from 'file-saver'
// 導(dǎo)出
exportExcel(){
const data = [
[ '姓名','得分']
['張三',100],
['李四',90]
];
const ws = XLSX.utils.aoa_to_sheet(data);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
const excelBuffer = XLSX.write(wb, { type: 'array', bookType: 'xlsx' });
const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
saveAs(blob, 'demo.xlsx');
}
效果