将 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`
  )
}
相关推荐
原则猫5 小时前
HOOKS 背后机制
前端
码语智行5 小时前
首页导航跳转功能深度解析-系统内和系统外
前端
阿猫的故乡6 小时前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
裕波6 小时前
Vue&ViteConf 2026 将于 7 月 18 日在上海举办,尤雨溪将现场发表主题演讲
vue.js·vite
IManiy6 小时前
总结之Vibe Coding前端骨架
前端
JS菌7 小时前
AI Agent 沙箱双层防护体系:从权限过滤到内核隔离的完整实现
前端·人工智能·后端
Aphasia3117 小时前
从输入URL到页面展示全流程
前端·面试
我叫黑大帅7 小时前
前端如何竖屏固定视口背景
前端·javascript·面试
abcy0712137 小时前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas
IT_陈寒8 小时前
Vite这个坑我帮你踩了,动态导入居然这样才生效
前端·人工智能·后端