0

in my project i have table with information (FirstName, LastName, Age, Date), so i create a function making me to export this data format excel. but in my case i don't want TO EXPORT all the information of the table, i want just ( LastName, Age).

this is my code :

client.html :

<div class="panel-body table-responsive">
    <table id="excel-table" class="table">
        <thead>
            <tr>
                <th>FirstName</th>
                <th>LastName</th>
                <th>Age</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor='let client of clients'>
                <td>{{client.FirstName}}</td>
                <td>{{client.LastName}}</td>
                <td>{{client.Age}}</td>
                <td>{{client.Date}}</td>
                <td>
                    <button class="btn btn-sm btn-danger" (click)="exportexcel()">ExportExcel</button>
                </td>
            </tr>
        </tbody>
    </table>
</div>

client.ts :

@Component({
  selector: 'app-clients',
  templateUrl: './clients.component.html',
  styleUrls: ['./clients.component.css']
})
export class ClientsComponent implements OnInit {


  constructor() { }

  fileName = 'ExcelFile.xlsx';

  exportexcel(): void {
    let element = document.getElementById('excel-table');
    const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(element);
    const wb: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
    XLSX.writeFile(wb, this.fileName);
  }
contact
  • 1
  • 1
  • 3

1 Answers1

0

Instead of storing the HTML table to Excel, you can transform the clients array to what you want to export (for example with Array.map()). Then you transform that data to a XLSX.Worksheet using XLSX.utils.json_to_sheet().

const data = this.clients.map(c => ({ 'LastName': c.LastName, 'Age': c.Age }));
const ws = XLSX.utils.json_to_sheet(data);
...
uminder
  • 23,831
  • 5
  • 37
  • 72