Вы можете использовать библиотеки Blob и file-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>
Во-первых, установите этот файл-заставку пакет NPM, используя npm i angular-file-saver
И в 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