Here is my example data:
exampleData: any[] = [
{
"id": "123",
"requestType": "Demo",
"requestDate": "12/05/21",
"status": "Success",
"product": [
{
"productName": "example product A",
"productQty": "8"
},
{
"productName": "example product B",
"productQty": "16"
}
]
}
]
.ts File:
toExportFileName(excelFileName: string): string {
var date = new Date();
return `${excelFileName}_${date.getMonth() + 1}${date.getDate()}${date.getFullYear()}.xlsx`;
}
exportAsExcelFile(json: any[], excelFileName: string): void {
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
XLSX.writeFile(workbook, this.toExportFileName(excelFileName));
}
exportToExcel() {
this.exportAsExcelFile(this.exampleData, 'Example_Report');
}
Actual Outcome:
https://i.sstatic.net/je7FR.png
Expected Outcome:
https://i.sstatic.net/K7hzq.png
I am modifying the logic to manage an array of objects and specify their positions in the Excel file. Can I achieve the desired outcome similar to the expected results provided above using the xlsx library with my example data??