若依vue 多table前端HTML页面导出一张Excel表

前言

导入依赖,具体前端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;

点个赞吧!

相关推荐
珹洺21 小时前
Bootstrap-HTML(二)深入探索容器,网格系统和排版
前端·css·bootstrap·html·dubbo
BillKu21 小时前
VS Code HTML CSS Support 插件详解
前端·css·html
xixixin_21 小时前
【React】中 Body 类限定法:优雅覆盖挂载到 body 的组件样式
前端·javascript·react.js
换日线°21 小时前
NFC标签打开微信小程序
前端·微信小程序
张3蜂1 天前
Python 四大 Web 框架对比解析:FastAPI、Django、Flask 与 Tornado
前端·python·fastapi
南风知我意9571 天前
【前端面试5】手写Function原型方法
前端·面试·职场和发展
qq_12498707531 天前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue云租车平台系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
小安驾到1 天前
【前端的坑】vxe-grid表格tooltip提示框不显示bug
前端·vue.js
去码头整点薯条981 天前
python第五次作业
linux·前端·python