Export Table to Excel ANGULAR

Asked

Viewed 16 times

-3

I wonder what I might be doing wrong in my code.

I need to export the table to Excel, but when I do that the table is empty, I have no idea where I went wrong.

Follows the code:

"code html"

<mat-table [dataSource]="dataSource" id="TableReport">
<!-- Description Cell Column -->
                                    

    <ng-container matColumnDef="descriptionCell">
           <mat-header-cell *matHeaderCellDef class="col-md-2">Descrição da Célula</mat-header-cell>
           <mat-cell *matCellDef="let element" class="col-md-2">{{element.descriptionCell}}</mat-cell>
    </ng-container>
    
    <!-- Description Deviation Column -->
    <ng-container matColumnDef="descriptionDeviation">
           <mat-header-cell *matHeaderCellDef class="col-md-2">Descrição do Desvio</mat-header-cell>
           <mat-cell *matCellDef="let element" class="col-md-2">{{element.descriptionDeviation}}</mat-cell>
    </ng-container>
    
    <!-- Description Inspector Column -->
    <ng-container matColumnDef="descriptionInspector">
           <mat-header-cell *matHeaderCellDef class="col-md-3">Descrição do Inspetor</mat-header-cell>
           <mat-cell *matCellDef="let element" class="col-md-3">{{element.descriptionInspector}}</mat-cell>
    </ng-container>
    
           <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
           <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    
     </mat-table>
     <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
    
     <div class="col-xs-8 filtrarButton">
          <a style="cursor: pointer" (click)="exportexcel()">
               <button type="button" class="botaoExcel">Excel</button>
          </a>

"code ts"

import * as XLSX from 'xlsx'; 

fileName= 'ReportExcel.xlsx';
  exportexcel(): void 
    {
       /* ID da tabela é passado aqui */   
       let element = document.getElementById('TableReport');
       const ws: XLSX.WorkSheet =XLSX.utils.table_to_sheet(element);

       /* Gerar pasta de trabalho e adicionar planilha */
       const wb: XLSX.WorkBook = XLSX.utils.book_new();
       XLSX.utils.book_append_sheet(wb, ws, 'ReportTeste');
       
       /* Salvar Arquivo */
       XLSX.writeFile(wb, this.fileName);
            
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.