使用XLSX.utils.table_to_book导出excel数据出现重复

1,571 阅读1分钟
var fix = document.querySelector('.el-table__fixed');
        var wb;
        let tempNode=document.querySelector('.tableFixedHeader') || document.querySelector('.reportTable')
        if (fix) {
          wb = XLSX.utils.table_to_book(tempNode.childNodes[0].removeChild(fix));
          tempNode.childNodes[0].appendChild(fix);
        } else {
          wb = XLSX.utils.table_to_book(document.querySelector('.ExportData'));
        }
      
        var wbout = XLSX.write(wb, {
          bookType: "xlsx",
          bookSST: true,
          type: "array",
        });
        try {
          //  name+'.xlsx'表示导出的excel表格名字
          FileSaver.saveAs(
            new Blob([wbout], { type: "application/octet-stream" }),
            name + ".xlsx"
          );
        } catch (e) {
          if (typeof console !== "undefined") console.log(e, wbout);
        }
        return wbout

因为在table中使用了fixed,element中,实现固定列是双层表格覆盖实现的,因此得到的table元素实际上有两个表格,导致导出的excel中有重复数据。 解决方式: 将上层fixed的表格从父元素中移除,等到得到要导出的数据后再添加回去。