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

点个赞吧!

相关推荐
胡萝卜术4 小时前
从API调用到手写LRU:力扣146「LRU缓存」的哈希表+双向链表终极进化之路
前端·javascript·面试
科技道人4 小时前
记录 默认置灰/禁用 app ‘Search Engine Selector‘ 的disable按钮
开发语言·前端·javascript
天疆说4 小时前
Zotero Connector 在 Edge 里找不到桌面 Zotero(Linux / Zotero 9.0.6 / Edge 150)
linux·前端·edge
李伟_Li慢慢5 小时前
02-从硬件说起WebGL
前端·three.js
kyriewen6 小时前
看了微软几万人用AI编程的数据——效率涨24%的代价没人提
前端·ai编程·claude
2601_963771376 小时前
How to Scale Your WordPress Store Traffic in 2026
前端·php·plugin
亿元程序员6 小时前
老板说我的3D箭头游戏用来做试玩太普通了,没人想玩,让我变点花样...
前端
李伟_Li慢慢7 小时前
01-threejs架构原理-课程简介
前端·three.js
_瑞8 小时前
深入理解 iOS 渲染原理
前端·ios
IT_陈寒8 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端