Vue+element-ui导出el-table中的数据

 1 //先安装依赖
 2 npm install --save xlsx file-saver
 3 
 4 //在要导出的vue组件中的script引入
 5 import FileSaver from "file-saver"
 6 import XLSX from "xlsx"
 7 
 8 
 9 exportExcel() {//执行此方法导出Excel表格
10     // 为el-table添加一个id:out-table
11     // 当el-table使用了fixed时会导出两次数据,所以要先进行判断
12     var fix = document.querySelector('.el-table__fixed');
13     var wb;
14     if (fix) {
15        // 如果有fixed,先移除,然后再添加上
16        wb = XLSX.utils.table_to_book(document.querySelector("#out-table").removeChild(fix))
17        document.querySelector("#out-table").appendChild(fix)
18     }else{
19        wb = XLSX.utils.table_to_book(document.querySelector("#out-table"))
20     }
21     var wbout = XLSX.write(wb, {
22        bookType: "xlsx",
23        bookSST: true,
24        type: "array"
25     });
26     try {
27        FileSaver.saveAs(
28          new Blob([wbout], {
29                type: "application/octet-stream"
30           }),
31           // 导出的文件名称
32           "Data.xlsx"
33        )
34     } catch (e) {
35        if (typeof console !== "undefined") console.log(e, wbout);
36     }
37     return wbout;
38 },

 

posted @ 2020-09-29 17:12  yw3692582  阅读(2115)  评论(0编辑  收藏  举报