项目中使用到,记录一下简单的demo
1、安装filesaver
npm install file-saver --save
2、导出方法
import { saveAs } from "file-saver"; //方法 exportTable() { const blob = new Blob([document.getElementById(‘exportableTable‘).innerHTML], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" }); saveAs(blob, ‘test.xls‘); }
3、html
<button mat-raised-button color="primary" (click)="exportTable()">导出</button> <div id="exportableTable" class="hidden-table"> <table> <thead> <tr> <th *ngFor="let item of tableTitle">{{item}}</th> </tr> </thead> <tbody> <tr *ngFor="let item of tableData"> <td *ngFor="let title of titleArr">{{item[title]}}</td> </tr> </tbody> </table> </div>