element-ui table导出excel
1、首先安装导出excel所需要的两个依赖包
这里下载的话最好是指定一下版本,不然会出现版本问题
npm install xlsx@^0.16.0 --save
npm install file-saver@^2.0.2 --save
2、下载完成后在指定的页面引入使用
import FileSaver from "file-saver";
import XLSX from "xlsx";
5、导出事件
另一种自定义表头
https://blog.csdn.net/qq_43923146/article/details/109779074?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166659283416800192255302%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=166659283416800192255302&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-2-109779074-null-null.nonecase&utm_term=excel&spm=1018.2226.3001.4450
总体代码
<template>
<div>
<el-button type="primary" @click="exportant">导出</el-button>
<el-table
id="out-table"
:data="tableData"
style="width: 100%">
<el-table-column
prop="date"
label="日期"
width="180">
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="180">
</el-table-column>
<el-table-column
prop="address"
label="地址">
</el-table-column>
</el-table>
</div>
</template>
<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
data() {
return {
tableData: [{
date: '100:200',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
}, {
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀区金沙江路 1517 弄'
}, {
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}, {
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄'
}]
}
},
methods:{
exportant(){
var xlsxParam = { raw: true };
/* 从表生成工作簿对象 */
var wb = XLSX.utils.table_to_book(
document.querySelector("#out-table"),
xlsxParam
);
/* 获取二进制字符串作为输出 */
var wbout = XLSX.write(wb, {
bookType: "xlsx",
bookSST: true,
type: "array",
});
try {
FileSaver.saveAs(
//Blob 对象表示一个不可变、原始数据的类文件对象。
//Blob 表示的不一定是JavaScript原生格式的数据。
//File 接口基于Blob,继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
//返回一个新创建的 Blob 对象,其内容由参数中给定的数组串联组成。
new Blob([wbout], { type: "application/octet-stream" }),
//设置导出文件名称
"sheetjs.xlsx"
);
} catch (e) {
if (typeof console !== "undefined") console.log(e, wbout);
}
return wbout;
}
}
}
</script>