1.引入2个必要的包
npm i xlsx file-saver -S
2.在需要导出表格的位置,引入这2个包
import FileSaver from "file-saver"
import XLSX from "xlsx"
3.对el-table加上id属性,注:作者试了ref,不能用
<el-table
id="mytable"
:data="userData"
border>
4.设置导出的事件行为
<el-button @click="output" >导出</el-button>
5.写output方法,直接复制就行了
output(){
var wb = XLSX.utils.table_to_book(document.querySelector("#mytable"));//mytable为表格的id名
/* get binary string as output */
var wbout = XLSX.write(wb, {
bookType: "xlsx",
bookSST: true,
type: "array"
});
try {
FileSaver.saveAs(
new Blob([wbout], { type: "application/octet-stream" }),
"sheet1.xlsx"//导出的表名
);
} catch (e) {
if (typeof console !== "undefined") console.log(e, wbout);
}
return wbout;
}
网友评论