在Angular中使用XLSX导出HTML表格时隐藏/删除行,可以通过以下步骤实现:
npm install xlsx file-saver --save
import * as XLSX from 'xlsx';
import { saveAs } from 'file-saver';
exportTableToExcel() {
const table = document.getElementById('tableId');
const tableData = [];
const rows = table.getElementsByTagName('tr');
for (let i = 0; i < rows.length; i++) {
const rowData = [];
const cells = rows[i].getElementsByTagName('td');
for (let j = 0; j < cells.length; j++) {
rowData.push(cells[j].innerText);
}
tableData.push(rowData);
}
this.exportToExcel(tableData);
}
exportToExcel(data: any[][]) {
const worksheet: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(data);
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
const excelBlob: Blob = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' });
saveAs(excelBlob, 'table.xlsx');
}
<table id="tableId">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
<!-- Add more rows here -->
</table>
<button (click)="exportTableToExcel()">Export to Excel</button>
通过以上步骤,你可以在Angular中使用XLSX库导出HTML表格时隐藏/删除行。请注意,这只是一个简单的示例,你可以根据实际需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云