angular - 使用 XLSX 导出 html 表时隐藏/删除列(表到表)
问题描述
我希望能够以 excel 文件格式导出我的 DOM 表。
我的功能(Typescript 3.5 / Angular 8)
ExportTOExcel() {
const ws: XLSX.WorkSheet=XLSX.utils.table_to_sheet(document.getElementById('serversTable'));
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Servers');
/* save to file */
XLSX.writeFile(wb, 'myproject.xls');
}
我的桌子:
<table id="serversTable" mat-table [dataSource]="serverArray" multiTemplateDataRows>
<ng-container matColumnDef="number">
<th mat-header-cell *matHeaderCellDef>Qty</th>
<td mat-cell *matCellDef="let resourceGroup">
...
</td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>name</th>
<td mat-cell *matCellDef="let resourceGroup">
...
</td>
</ng-container>
<!-- Other columns -->
<ng-container matColumnDef="expandedDetail">
...
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let resourceGroup; columns: displayedColumns;" class="example-
element-row" [class.example-expanded-row]="expandedElement === resourceGroup
(click)="clickOnRowDisk(resourceGroup)">
</tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>
这expandedDetail
是我不希望它出现在函数写入的文件中的列ExportTOExcel
。此列是我的表格的最后一列。
我试过了
ws['!cols'] = [];
ws['!cols'][0] = { hidden: true };
但不起作用。
解决方案
隐藏列
const ws:
XLSX.WorkSheet=XLSX.utils.table_to_sheet(document.getElementById('serversTable'));
ws['!cols'] = [];
ws['!cols'][12] = { hidden: true };
/* here 12 is your column number (n-1) */
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Servers');
/* save to file */
XLSX.writeFile(wb, 'myproject.xls');
推荐阅读
- javascript - Javascript (target.className) 如果元素有多个类名
- javascript - dayjs 我只想做数字
- firebase - 同一文档中各个字段的多个并发更新是否需要事务?
- python - 如何从较大的元素列表列表中删除较小的元素列表列表
- python - 已计算 Z 时,如何在 matplotlib 中创建曲面图?
- python - Ajax POST 被 CORS 策略阻止
- javascript - ReactJS:一位数字输入可以以多个“0”结尾
- office-js - OfficeJS Outlook 邮件正文 getasync 为
标签
- java - 如何从 postgresql 中的存储过程中获取值到 java 应用程序中?我不断收到错误
- python - 如何从 BeautifuleSoup Python 创建的输出 CSV 文件中删除一种数据