安装插件:npm install xlsx file-saver --save
"xlsx": "^0.18.5", "file-saver": "^2.0.5"
index.vue
import { saveAs } from 'file-saver'; import * as XLSX from 'xlsx';
<el-button ref="search_s_btn" type="primary" @click="downloadxlx()">导出</el-button>
downloadxlx(){
//el-table 写上id="table-content"
const et = XLSX.utils.table_to_book(document.getElementById('table-content'), { raw: true }); //此处传入table的DOM节点 注意 Id 不要重复
const fileName = '测试数据表.xlsx'; //文件名称
let etout = XLSX.write(et, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
});
try {
let blobObj = null;
saveAs(
(blobObj = new Blob([etout], {
type: 'application/octet-stream;charset=utf-8'
})),
fileName
);
} catch (e) {
console.log(e, etout);
}
return etout;
}
},