Element 將表格中的數據導出


1、需求(需要將查詢的數據導出)

 

 

 2、代碼

 1 <!-- 表格 -->
 2     <el-table
 3       id="out-table"
 4       :data="data.slice((currentPage - 1) * limit, currentPage * limit)"
 5       style="width: 100%;"
 6       tooltip-effect="dark"
 7       stripe
 8       height="520"
 9       @selection-change="handleSelectionChange"
10       border
12     >
13       <el-table-column type="selection" width="55"> </el-table-column>
14       <el-table-column prop="datetime" label="日期" fixed>
15         <template slot-scope="scope">
16           <span>{{ scope.row.datetime }}</span>
17         </template>
18       </el-table-column>
19       <!-- 詳細信息 -->
20       <el-table-column
21         v-for="(item, index) in tableHead"
22         :label="item"
23         :key="index"
24       >
25         <template slot-scope="scope">
26           {{ scope.row.data[item] }}
27         </template>
28       </el-table-column>
29     </el-table>
30 <!-- 導出 -->
31 <el-button type="primary" icon="el-icon-download" @click="downloadExcel">導出</el-button>
<!-- 分頁 -->
    <div class="block page1" v-show="isFlag">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[15, 30, 45, 60]"
        :page-size="limit"
        layout="sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </div>

  

 1 data() {
 2     return {
 3       data: [],
 4       tableHead: [],
 5       deviceList: [],
 6       getAllData: [],
 7       limit: 15, //每頁顯示15條數據
 8       currentPage: 1, //當前頁,從第一頁開始
 9       total: 0, //總條目數
10       multipleSelection: [], //選中的數據
11       excelData: []
12     };
13   },
1 // 分頁------每頁條數切換
2     handleSizeChange(val) {
3       this.limit = val;
4     },
1  // 當前頁改變時觸發
2     handleCurrentChange: function (currentPage) {
3       this.currentPage = currentPage;
4       // console.log(`當前頁: ${val}`);
5     },

 

數據寫入

 1 downloadExcel() {
 2       this.$confirm("確定下載列表文件?", "提示", {
 3         confirmButtonText: "確定",
 4         cancelButtonText: "取消",
 5         type: "warning",
 6       })
 7         .then(() => {
 8           this.excelData = this.multipleSelection;
 9           this.exportExcel();
10         })
11         .catch(() => {});
12     },
13     // 數據寫入Excel
14     exportExcel(){
15       var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
16         /* 獲取二進制字符串作為輸出 */
17         var wbout = XLSX.write(wb, {
18             bookType: "xlsx",
19             bookSST: true,
20             type: "array"
21         });
22         try {
23             FileSaver.saveAs(
24             //Blob 對象表示一個不可變、原始數據的類文件對象。
25             //Blob 表示的不一定是JavaScript原生格式的數據。
26             //File 接口基於Blob,繼承了 blob 的功能並將其擴展使其支持用戶系統上的文件。
27             //返回一個新創建的 Blob 對象,其內容由參數中給定的數組串聯組成。
28             new Blob([wbout], { type: "application/octet-stream" }),
29             //設置導出文件名稱
30             "歷史記錄.xlsx"
31             );
32         } catch (e) {
33             if (typeof console !== "undefined") console.log(e, wbout);
34         }
35         return wbout;
36         },

選中表數據:

1 // 選中的表的數據
2     handleSelectionChange(val) {
3       this.multipleSelection = val;
4       console.log(this.multipleSelection);
5     },

 


免責聲明!

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



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