前言
导入依赖,具体前端vue配置就不介绍了,直接晒具体细节代码
实现
bash
需要在多table外加div,其他都是基本操作
js代码
html
import FileSaver from 'file-saver'
import * as XLSX from "xlsx";
const htmlToExcel = {
getExcelNew(className, excelName) {
var xlsxParam = {raw: true}; // 导出的内容只做解析,不进行格式转换
// 创建新的表格元素并插入到DOM中
var newTable = document.createElement('table');
newTable.innerHTML = document.querySelector("#table2");
newTable.innerHTML = document.querySelector("#table3");
newTable.innerHTML = document.querySelector("#table4");
newTable.innerHTML = document.querySelector("#table5");
newTable.innerHTML = document.querySelector("#table6");
newTable.innerHTML = document.querySelector("#table7");
document.body.appendChild(newTable);
let fix = document.querySelector('.el-table__fixed');
// document.querySelector('.el-picker-panel').innerHTML = "";
// document.querySelector('.el-picker-panel').parentNode.removeChild(document.querySelector('.el-picker-panel'));
let wb;
if (fix) { //判断要导出的节点中是否有fixed的表格,如果有,转换excel时先将该dom移除,然后append回去
wb = XLSX.utils.table_to_book(document.querySelector(className).removeChild(fix));
document.querySelector(className).appendChild(fix);
} else {
wb = XLSX.utils.table_to_book(document.querySelector("#tableApp"));
}
var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'});
try {
FileSaver.saveAs(new Blob([wbout], {type: 'application/octet-stream'}), excelName + ".xlsx")
} catch (e) {
if (typeof console !== 'undefined') {
}
}
return wbout
},
};
export default htmlToExcel;
点个赞吧!