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

点个赞吧!

相关推荐
Liora_Yvonne3 分钟前
不懂后端,只会 TypeScript,想独立做完整项目?这套全栈底座就是给前端准备的
前端·后端·全栈
前端Hardy5 分钟前
GitHub 爆火!236K+ Star!一套 Skills 让 AI 按工程师方式写代码
前端·后端
烬羽11 分钟前
AI Coding 全流程实战:从需求到上线,我用 AI 开发了一个 NPM 包
前端·react.js·ai编程
前进的程序员11 分钟前
Codex破局:前端组件秒级生成|提速React/Vue开发,可复用提示词+实战调试经验
前端·vue.js·react.js·codex
前端snow34 分钟前
ai agent ---output汇总
前端
4311媒体网36 分钟前
帝国CMS网站自定义搭建技巧
服务器·开发语言·前端
JunjunZ38 分钟前
Vue 3 文件预览的通用设计与实现
前端·vue.js
CappuccinoRose39 分钟前
ES6+ 核心语法糖
前端·es6·解构·可选链·空值合并
两只羊ovo1 小时前
Promise.all 并发请求实战:从串行 await 到双段并行
前端
七牛开发者1 小时前
解读 Cordis:dsh 一切皆插件背后的运行时设计
前端·javascript·后端