一、安装依赖
npm install --save xlsx file-saver
二、在需要导出成excel的文件里面引入依赖
三、methods里面写个导出的方法
exportExcel(excelName) {
//excelName --设置导出的excel名称
//report-table --对应的要导出的el-table的ref名称
try {
const $e = this.$refs['report-table'].$el;
// 如果表格加了fixed属性,则导出的文件会生产两份一样的数据,所以可在这里判断一下
let $table = $e.querySelector('.el-table__fixed');
if (!$table) {
$table = $e;
}
// 为了返回单元格原始字符串,设置{ raw: true }
const wb = XLSX.utils.table_to_book($table, { raw: true });
const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
FileSaver.saveAs(
new Blob([wbout], { type: 'application/octet-stream' }),
`${excelName}.xlsx`,
);
} catch (e) {
if (typeof console !== 'undefined') console.error(e);
}
},
四、参考文档
https://blog.csdn.net/u010427...
https://github.com/SheetJS/js-xlsx
https://github.com/eligrey/FileSaver.js
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。