JS Excel导出代码正在从报告内容中删除所有空白

问题描述 投票:0回答:1

我正在编写JS代码以导出公司报告的动态集合。我已经有了将所有报告编译成一个大型excel文档的代码,但是由于某种原因,导出的文件删除了报告中每个单元中每个单词之间的所有空格。

在导出之前,我已经回显了已编译的表,看起来还不错。

//Each report I'm compiling into one large table has the typical arrangement of: 

<table>
<thead> - <th> - </th> (dynamic number) </thead> 
<tbody> - <td> - </td> (dynamic number) - </tbody> 
</table>

function loadEXCEL(tableID){
    //Gather Data
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);

    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
    //console.log(tableHTML);

    // Create download link element
    downloadLink = document.createElement("a");

    document.body.appendChild(downloadLink);

    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, thisFileName);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

        // Setting the file name
        downloadLink.download = thisFileName;

        //triggering the function
        downloadLink.click();
    }
  }

什么会导致Blob在导出时删除每个单元格中的所有空格?

再一次,我要先回声最后一张大桌子,那里是空格。我以某种方式认为这是Blob的错,但我不知道。

如何阻止导出过程中删​​除每个单词之间的空格?

想法?

javascript php export xls
1个回答
0
投票

我已经检查了您的代码,并将其与示例数据一起添加到代码笔中https://codepen.io/konutis/pen/RwwmXwV?editors=1010

我有MacBook,所以只能在“ Numbers”应用程序中打开此文件。

我看到的是这个-

enter image description here

但是在Excel中打开时,空格也有类似的问题,没有空格。

我用FileSaver.js插件解决了。这是一个小型开源插件,可以为您节省所有费用。这是一个库-https://github.com/eligrey/FileSaver.js/

您要做的就是添加这个库,然后运行它

const tableText = document.querySelector('yourTableSelectorHere').innerText;
saveAs(new Blob([tableText], {type: 'application/vnd.ms-excel;charset=utf-8'}), 'table.xls', true);

注意:tableText变量是表中的内部文本(不是HTML)。

它对我有所帮助,希望它也对您有所帮助。这是一个工作示例-https://codepen.io/konutis/pen/dyyEEbm

© www.soinside.com 2019 - 2024. All rights reserved.