使用onclick函数后找不到导出文件

时间:2019-09-20 09:23:45

标签: javascript excel angular

我有一个名为exportExcel的函数,可通过单击按钮将表导出到excel文件,该按钮可以正常工作,并且可以识别该文件,但它显示为“ xxx.xls-Echec-未找到文件”,有人吗?请修复吗?这是我的职能,也是我在html文件中称呼她的地方

home.component.ts

exportExcel(table1, filename = ''){

    let downloadLink;
    let dataType ='application/vnd.ms-excel';
    let tableselect = document.getElementById('table1');
    let tableHTML = tableselect.innerHTML.replace(/ /g,'%20');

    //Filename
    filename = filename?filename+'.xls':'CPF.xls';

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

    document.body.appendChild(downloadLink);

    if(navigator.msSaveOrOpenBlob){
      let blob = new Blob(['\ufeff', tableHTML],{
        type: dataType
      });
      navigator.msSaveOrOpenBlob(blob, filename);
    } else {
      //Link to file
      downloadLink.href = 'data' + dataType +',' +tableHTML;

      //File name
      downloadLink.download = filename;

      //Triggering function

      downloadLink.click();
    }
  }

home.component.html

<mat-tab>
      <button class="bCopy2" (click)="exportExcel('tblData')"> Export to excel</button>

      <div id="cp">
      <table id="table1">
        <thead>
        <tr>
          <td>name</td>
          <td>firstname</td>
          <td>id</td>
          <td>adress</td>
        </tr>
        </thead>
        <tbody>
        <tr>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
        </tr>
        </tbody>
      </table>
      </div>
    </mat-tab>

编辑:

我使用此功能通过使用jQuery table2excel插件使其工作:

    $('.bCopy2').on('click', function(e){
      e.preventDefault();
      ResultsToTable();
    });

    function ResultsToTable(){
      $(".table1").table2excel({
        exclude: ".noExl",
        name: "Results"
      });
    }
  });

0 个答案:

没有答案