vue中el-table前端如何導(dǎo)出excel數(shù)據(jù)表格
一、el-table為正常時(shí),導(dǎo)出方法如下:

1.添加導(dǎo)出按鈕
<el-button class="greenLinearbg dc" size="small" @click="webExportTotalExcel()" v-if="totalBillShow">導(dǎo)出</el-button>
2.導(dǎo)出方法
// web導(dǎo)出匯總單excel
webExportTotalExcel(){
// 獲取表格數(shù)據(jù)
//const tableData = this.userTotalList;
const tableData = this.userTotalList.map(row => { //創(chuàng)建一個(gè)新的數(shù)組,處理null值
return Object.keys(row).reduce((acc, key) => {
acc[key] = row[key] === null ? '' : row[key];
return acc;
}, {});
});
// 構(gòu)建 Excel 文件內(nèi)容
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>';
// 添加表格數(shù)據(jù)
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>';
}
// 構(gòu)建完整的 Excel 文件內(nèi)容
excelContent += '</table></body></html>';
// 創(chuàng)建 Blob 對(duì)象
const blob = new Blob([excelContent], { type: 'application/vnd.ms-excel' });
// 創(chuàng)建鏈接并觸發(fā)下載
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = '匯總單.xlsx'; // 設(shè)置默認(rèn)文件名
link.style.display = "none";
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(link.href);
},二、el-table中列為循環(huán)數(shù)據(jù)時(shí),如下圖所示:


導(dǎo)出方法如下:
1.導(dǎo)出按鈕:
<el-button class="greenLinearbg dc" size="small" @click="webExportHistoryExcel('','歷史賬單','el-table__fixed-right',0,'message')" v-if="historyBillShow">導(dǎo)出</el-button>2.導(dǎo)出方法為:
// web導(dǎo)出歷史賬單excel
webExportHistoryExcel(id,excelName,className,number=0){
const loading = this.$loading({
lock: true,
text: '數(shù)據(jù)導(dǎo)出中',
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();
}
},到此這篇關(guān)于vue中el-table前端導(dǎo)出excel數(shù)據(jù)表格的文章就介紹到這了,更多相關(guān)vue el-table導(dǎo)出excel數(shù)據(jù)表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Vue微信授權(quán)登錄前后端分離較為優(yōu)雅的解決方案
這篇文章主要介紹了詳解Vue微信授權(quán)登錄前后端分離較為優(yōu)雅的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native
今天小編就為大家分享一篇vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue3 Element Plus中icon圖標(biāo)不顯示的解決方案
這篇文章主要介紹了vue3 Element Plus中icon圖標(biāo)不顯示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

