Element-ui组件库Table表格导出Excel表格--存在重复数据问题

借鉴:https://www.jianshu.com/p/1971fc5b97ca

https://blog.csdn.net/qq_40614207/article/details/94003793

贴出代码

  // 定义导出Excel表格事件
      exportExcel() {
        // 解决生成重复数据-因为使用l fixed属性
        var fix = document.querySelector('.el-table__fixed')
        var wb
        // 判断要导出的节点中是否有fixed的表格,如果有,转换excel时先将该dom移除,然后append回去
        if (fix) {
          /* 从表生成工作簿对象 */
          wb = XLSX.utils.table_to_book(document.querySelector('#out-table').removeChild(fix))
          document.querySelector('#out-table').appendChild(fix)
        } else {
          wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
        }
        /* 获取二进制字符串作为输出 */
        var wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        try {
          FileSaver.saveAs(
            // Blob 对象表示一个不可变、原始数据的类文件对象。
            // Blob 表示的不一定是JavaScript原生格式的数据。
            // File 接口基于Blob,继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
            // 返回一个新创建的 Blob 对象,其内容由参数中给定的数组串联组成。
            new Blob([wbout], { type: 'application/octet-stream' }),
            // 设置导出文件名称
            'sheetjs.xlsx'
          )
        } catch (e) {
          if (typeof console !== 'undefined') console.log(e, wbout)
        }
        return wbout
      },

  

猜你喜欢

转载自www.cnblogs.com/fdxjava/p/12335609.html