一、el-table為正常時,導出方法如下:
1.添加導出按鈕
<el-button class="greenLinearbg dc" size="small" @click="webExportTotalExcel()" v-if="totalBillShow">導出</el-button>
2.導出方法
// web導出匯總單excelwebExportTotalExcel(){// 獲取表格數據//const tableData = this.userTotalList;const tableData = this.userTotalList.map(row => { //創建一個新的數組,處理null值return Object.keys(row).reduce((acc, key) => {acc[key] = row[key] === null ? '' : row[key];return acc;}, {});});// 構建 Excel 文件內容let excelContent = `<html><head><meta charset="UTF-8"></head><body><table border="1">`;// 添加表頭excelContent += '<tr>';for (const column of this.$refs.tableRef.columns) {if (column.property) {excelContent += `<th>${column.label}</th>`;}}excelContent += '</tr>';// 添加表格數據for (const row of tableData) {excelContent += '<tr>';for (const column of this.$refs.tableRef.columns) {if (column.property) {excelContent += `<td>${row[column.property]}</td>`;}}excelContent += '</tr>';}// 構建完整的 Excel 文件內容excelContent += '</table></body></html>';// 創建 Blob 對象const blob = new Blob([excelContent], { type: 'application/vnd.ms-excel' });// 創建鏈接并觸發下載const link = document.createElement("a");link.href = URL.createObjectURL(blob);link.download = '匯總單.xlsx'; // 設置默認文件名link.style.display = "none";document.body.appendChild(link);link.click();window.URL.revokeObjectURL(link.href);},
二、el-table中列為循環數據時,如下圖所示:
導出方法如下:
1.導出按鈕:
<el-button class="greenLinearbg dc" size="small" @click="webExportHistoryExcel('','歷史賬單','el-table__fixed-right',0,'message')" v-if="historyBillShow">導出</el-button>
2.導出方法為:
// web導出歷史賬單excelwebExportHistoryExcel(id,excelName,className,number=0){const loading = this.$loading({lock: true,text: '數據導出中',spinner: 'el-icon-loading',background: 'rgba(0, 0, 0, 0.7)'});const columns = this.$refs.message.columns,outputColumns=[];for(let i=0,len=columns.length;i<len;i++){if('label' in columns[i]){if('prop' in columns[i])outputColumns.push(columns[i].prop);else if('slot' in columns[i] && columns[i].slot!='action' && columns[i].label!='操作')outputColumns.push(columns[i].slot);}}if(this.$refs.message.selectRow=='all'){request({url:'/system/nonResidentYhzd/selectYhzdTable',method:'post',data:{pageNum:1,pageSize:this.queryParams.total}}).then(response => {ExportUtils.exportExcel(id,excelName,'',className,number,this.$refs.message.selectRow,response.rows,outputColumns);loading.close();});}else{ExportUtils.exportExcel(id,excelName,'',className,number,this.$refs.message.selectRow,this.$refs.message.selectList,outputColumns);loading.close();}},