element-table表格元件分頁後完整匯出到excel的方法
阿新 • • 發佈:2020-11-29
首先表格匯出需要使用到js-xlsx庫,vue通過npm install xlsx安裝後使用important XLSX from 'xlsx'匯入
程式碼片段:
----------------html------------------------------
<el-buttonclass="btn"type="primary"@click="onExport">匯出</el-button><el-table ref="filterTable" @row-click="rowClickHandle":data="tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)" //資料分頁顯示 > <el-table-column show-overflow-tooltip prop="name" label="名稱" ></el-table-column> <el-table-columnshow-overflow-tooltip prop="address" label="地址" ></el-table-column> <el-table-column show-overflow-tooltip label="狀態"> <template slot-scope="scope"> <span v-if="scope.row.statu == 0">待完成</span> <span v-if="scope.row.statu == 1">已完成</span> </template> </el-table-column> <el-table-column show-overflow-tooltip label="操作" v-if="oprationShow" > <template slot-scope="scope"> <span style="cursor: pointer" @click.stop="edit(scope)" >編輯</span > <span style="cursor: pointer" @click.stop="del(scope)" >刪除</span > </template> </el-table-column> </el-table> <el-pagination layout="total,prev, pager, next" :total="total" :page-size="pageSize" @current-change="changePage" > </el-pagination>
------------------------js--------------------------------
onExport() { this.oprationShow = false //不顯示操作按鈕 this.pageSize = this.total //將表格長度變成資料總長度 this.currentPage = 1 //顯示第一頁 this.$nextTick(function () { let wb = XLSX.utils.table_to_book(this.$refs.filterTable.$el); let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' }); try { _global.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '表格.xlsx') } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) } this.pageSize = 13 //表格長度還原 this.oprationShow = true //顯示操作按鈕 return wbout; }) },changePage(currentPage){ this.currentPage=currentPage; },
遇到的問題:
1.js-xlsx方法只能匯出表格dom元素所顯示的資料,分頁的資料無法匯出
解決方法:將表格在匯出時不進行分頁,所有資料都顯示在第一頁
2.表格的操作按鈕不需要匯出
解決辦法:將表格匯出時隱藏操作按鈕當前列