My project involves managing a table of information that includes fields for FirstName, LastName, PhoneNumber, Age, and Date. I've created a function that allows me to export the data to an Excel file, but I only want to export specific fields like FirstName, LastName, and Age.
This is how I achieved that:
excel.html :
<div class="panel-body table-responsive">
<table id="excel-table" class="table">
<thead>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>PhoneNumber</th>
<th>Age</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let excel of excels'>
<td>{{excel.FirstName}}</td>
<td>{{excel.LastName}}</td>
<td>{{excel.PhoneNumber}}</td>
<td>{{excel.Age}}</td>
<td>{{excel.Date}}</td>
<td>
<button (click)="exportexcel()">ExportExcel</button>
</td>
</tr>
</tbody>
</table>
</div>
excel.ts :
@Component({
selector: 'app-execls',
templateUrl: './execls.component.html',
styleUrls: ['./execls.component.css']
})
export class RegionsComponent implements OnInit {
constructor(private modalService: NgbModal, private fb: FormBuilder, private toastr: ToastrService) { }
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);
}