c# - Angular 5中的excel上传

标签 c# angular5

我正在尝试使用以下代码将数据导出到 angular 5 中。 Excel 下载成功。但是 excel 中的数据显示如下:

something like this

downloadReport(data:any){
        let blob = new Blob([data], {  type:'text/html' });
        let url= window.URL.createObjectURL(blob);
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.href = url;
        a.download = "newfile.xls";
        a.click();
        // window.open(url);
    }

最佳答案

您可以使用 Blobfile-saver导出excel文件中表格数据的库。

在 HTML 中:

<table id="exportable" *ngIf="data.length != 0" cellpadding="1" cellspacing="1" class="table table-bordered table-striped">
      <thead>
          <tr>
              <th>Name</th>
              <th>Username</th>
          </tr>
      </thead>
      <tbody>
          <tr *ngFor="let report of data">
              <td>{{report.name }}</td>
              <td>{{report.userName}}</td>
          </tr>
      </tbody>
  </table>
  <button mat-button (click)="exportToExcel()">Download Excel</button>

首先,安装 this file-saver使用 npm i angular-file-saver

的 NPM 包

在 TS 中:

import { saveAs } from 'file-saver'; /* import */

和函数:

exportToExcel() {
    var blob = new Blob([document.getElementById("exportable").innerText], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
    });
    var fileName = 'Test.xls';
    saveAs(blob, fileName);
  }

StackBlitz Example

关于c# - Angular 5中的excel上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52676854/

相关文章:

javascript - Angular 5 在第一次编译时出现错误(错误 : TS2339),,但编译成功后一切正常

css - Jqwidgets 百分比宽度不适用于 Angular-5

c# - 将 IAuthorizationFilter 与 Ninject 和 EF 一起使用会给出 DbContext has been disposed 错误

c# - 如何在列表框中显示替代对象的表示?

Angular 5 动画不适用于 iOS 10.3 Safari

angular - 如何导航到父路由(或清除路由器导出)? Angular 5

Angular 5 HttpClient 路径变量

c# - 检查重复项时的性能

javascript - 将数据从 HTTPS 页面发送到本地主机服务器

c# - 使用 LINQ to Entities 向表中插入多条记录的正确方法