el-table合并内容相同的表头、单元格,利用file-saver和XLSX纯前端导出多级表头表格,列宽根据内容长度动态设置,可直接复制

本文介绍如何在Vue.js项目中使用ElementUI实现el-table的表头和单元格合并,以及如何借助file-saver和XLSX库进行表格的导出,动态设置列宽,实现多级表头的Excel文件生成。
摘要由CSDN通过智能技术生成

合并 

表头合并:el-table中加上这个方法

  :header-cell-style="handerMethod"  

 方法:

// 合并表头这里合并的是2、3

    handerMethod({ row, column, rowIndex, columnIndex }) {

      console.log(row,'row');

      if (row[1].level === 1) {

        // 这里有个非常坑的bug 必须是row[0]=0 row[1]=2才会生效

        row[1].colSpan = 0

        row[2].colSpan = 2

        if (columnIndex === 1) {

          return { display: 'none' }

        }

      }

    },

单元格合并:和表头合并在同个位置加上这个方法: :span-method="genderSpanCity"

方法:

genderSpanCity({ row, column, rowIndex, columnIndex }) {

      if (columnIndex === 0 || columnIndex === 1 ) {//第一列和第二列有相同的就合并

        const cacheKey = `${rowIndex}-${columnIndex}`

        if (this.cache[cacheKey]) {

          return this.cache[cacheKey]

        }

        const currentValue = row[column.property]

        const preRow = this.tableData[rowIndex - 1]

        const preValue = preRow ? preRow[column.property] : null

        if (currentValue === preValue) {

          this.cache[cacheKey] = { rowspan: 0, colspan: 0 }

        } else {

          let rowspan = 1

          for (l

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值