Reputation: 7785
I'm trying to export some JSON data from my Angular to Excel. I already did it successfully. My problem is how would i include the headings to the excel also? I can only export the table but not the headings? Please see the link and codes below
CODE
public exportAsExcelFile(json: any[], excelFileName: string): void {
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
console.log('worksheet',worksheet);
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
//const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer' });
this.saveAsExcelFile(excelBuffer, excelFileName);
}
private saveAsExcelFile(buffer: any, fileName: string): void {
const data: Blob = new Blob([buffer], {
type: EXCEL_TYPE
});
FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION);
}
PIC
Upvotes: 1
Views: 968
Reputation: 1158
You need to add the following code once your have created your sheets
XLSX.utils.sheet_add_json(worksheet, [
{ A: '', B: '', C: ''},
], { header: ['A', 'B', 'C'], skipHeader: true, origin: -1 });
this.sheetHeaders(worksheet);
Updated stackblitz
Updated stackblitz 2
Upvotes: 1