将 Element UI 表格元素导出为 Excel 文件(处理了多级表头和固定列导出的问题)

javascript 复制代码
import { saveAs } from 'file-saver'
import XLSX from 'xlsx'
/**
 * 将 Element UI 表格元素导出为 Excel 文件
 * @param {HTMLElement} el - 要导出的 Element UI 表格的 DOM 元素
 * @param {string} filename - 导出的 Excel 文件的文件名(不包含扩展名)
 */
export function exportElementTable(el, filename) {
  // 深拷贝表格元素避免污染原始DOM
  const clonedEl = el.cloneNode(true)

  // 移除fixed列容器(避免重复内容)
  const fixedElements = clonedEl.querySelectorAll('.el-table__fixed, .el-table__fixed-right, .el-table__fixed-left')
  if (fixedElements && fixedElements.length > 0) {
    for (let i = 0; i < fixedElements.length; i++) {
      const fixedEl = fixedElements[i]
      if (fixedEl.parentNode) {
        fixedEl.parentNode.removeChild(fixedEl)
      }
    }
  }

  // 获取原始表头和表体的核心table元素
  const headerTable = clonedEl.querySelector('.el-table__header-wrapper table')
  const bodyTable = clonedEl.querySelector('.el-table__body-wrapper table')

  // 创建新表格容器(保留原始table的class和样式)
  const mergedTable = document.createElement('table')
  if (headerTable && headerTable.className) {
    mergedTable.setAttribute('class', headerTable.className)
  }

  // 复制表头结构(关键:保留多级表头的tr层级)
  if (headerTable) {
    const thead = document.createElement('thead')
    const headerRows = headerTable.getElementsByTagName('tr')
    if (headerRows && headerRows.length > 0) {
      for (let i = 0; i < headerRows.length; i++) {
        const tr = headerRows[i]
        thead.appendChild(tr.cloneNode(true))
      }
    }
    mergedTable.appendChild(thead)
  }

  // 复制表体结构(保留数据行)
  if (bodyTable) {
    const tbody = document.createElement('tbody')
    const bodyRows = bodyTable.getElementsByTagName('tr')
    if (bodyRows && bodyRows.length > 0) {
      for (let i = 0; i < bodyRows.length; i++) {
        const tr = bodyRows[i]
        tbody.appendChild(tr.cloneNode(true))
      }
    }
    mergedTable.appendChild(tbody)
  }

  // 关键修复:重新计算并保留合并单元格属性
  const allCells = mergedTable.getElementsByTagName('th')
  const dataCells = mergedTable.getElementsByTagName('td')
  const combinedCells = [].concat(Array.from(allCells), Array.from(dataCells))

  for (let i = 0; i < combinedCells.length; i++) {
    const cell = combinedCells[i]
    if (cell) {
      const rowSpan = cell.getAttribute('data-rowspan') || 1
      const colSpan = cell.getAttribute('data-colspan') || 1
      if (rowSpan > 1) {
        cell.setAttribute('rowspan', rowSpan)
      }
      if (colSpan > 1) {
        cell.setAttribute('colspan', colSpan)
      }
    }
  }

  // 转换配置(关键:启用display模式保留表格结构)
  const workbook = XLSX.utils.table_to_book(mergedTable, {
    raw: true,
    display: true, // 启用display模式,正确解析合并单元格和层级
    cellDates: true, // 保留日期格式(可选)
  })

  // 生成并保存文件
  const wbout = XLSX.write(workbook, {
    bookType: 'xlsx',
    bookSST: true,
    type: 'array',
  })

  saveAs(
    new Blob([wbout], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }),
    `${filename}.xlsx`
  )
}
相关推荐
ssshooter22 分钟前
你知道怎么用 pnpm 临时给某个库打补丁吗?
前端·面试·npm
IT利刃出鞘1 小时前
HTML--最简的二级菜单页面
前端·html
yume_sibai1 小时前
HTML HTML基础(4)
前端·html
给月亮点灯|2 小时前
Vue基础知识-Vue集成 Element UI全量引入与按需引入
前端·javascript·vue.js
知识分享小能手2 小时前
React学习教程,从入门到精通,React 组件生命周期详解(适用于 React 16.3+,推荐函数组件 + Hooks)(17)
前端·javascript·vue.js·学习·react.js·前端框架·vue3
面向星辰2 小时前
html音视频和超链接标签,颜色标签
前端·html·音视频
lxh01132 小时前
LRU 缓存
开发语言·前端·javascript
yangzhi_emo3 小时前
ES6笔记5
前端·笔记·es6
wow_DG3 小时前
【Vue2 ✨】Vue2 入门之旅 · 进阶篇(二):虚拟 DOM 与 Diff 算法
开发语言·javascript·vue.js·算法·前端框架