Hide/Delete column when exporting html table with XLSX (table to sheet)

Discussão em 'Angular' iniciado por licer93, Novembro 26, 2024.

  1. licer93

    licer93 Guest

    I want to be able to export in excel files format my tables of my DOM.

    My function (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');
    }


    My table :

    <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>


    The expandedDetail is the column that I don't want it to be in the file written by the function ExportTOExcel. This column is the last column of my table.

    I tried

    ws['!cols'] = [];
    ws['!cols'][0] = { hidden: true };


    but doesn't work.

    Continue reading...

Compartilhe esta Página