若依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;

点个赞吧!

相关推荐
IT_陈寒8 分钟前
Redis 性能翻倍的 7 个冷门技巧,第 5 个大多数人都不知道!
前端·人工智能·后端
mCell7 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip7 小时前
Node.js 子进程:child_process
前端·javascript
excel10 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel11 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼13 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping13 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙14 小时前
[译] Composition in CSS
前端·css
白水清风14 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix14 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts