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 },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM