We have used Swimlane-ngxtable for displaying data.
I want to export the data to excel.
I have tried following code :
private wrapAndCenterCell(range: XLSX.Range, worksheet: XLSX.WorkSheet) {
const wrapAndCenterCellStyle = {
alignment: { wrapText: true, vertical: 'center', horizontal: 'center' },
font: { bold: true, color: 'red' }
};
for (var R = range.s.r; R <= range.e.r; ++R) {
for (var C = range.s.c; C <= range.e.c; ++C) {
var cell_address = { c: C, r: R };
var cell_ref = worksheet[XLSX.utils.encode_cell(cell_address)];
this.setCellStyle(cell_ref, wrapAndCenterCellStyle);
}
}
}
private setCellStyle(cell: XLSX.CellObject, style: {}) {
cell.s = style;
}
private saveAsExcelFile(buffer: any, fileName: string): void {
const data: Blob = new Blob([buffer], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"
});
FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + ".xlsx");
}
I am able to export the data but no formatting is applied.
I want to know the optimized way to do it.
Follow this instruction if you want to export data to excel file in your project.
1.Add file-saver and xlsx dependencies
2.Implement ExcelService