#

Sunny Kushwaha

Admin

Angular Excel format Hack

1: Create excel-format.component.ts: You can Format Excel file through your angular code. Cosider a JSON or Array of object which can be converted to axcel file with custom formate. First install file-saver library and import it in your component.ts file. Here's an example of how the component may look like:

excel-format.component.ts
                                    
                                        import { Component } from '@angular/core';
                                        import * as FileSaver from 'file-saver';

                                        @Component({
                                            selector: 'app-excel-format',
                                            templateUrl: './excel-format.component.html'
                                        })
                                        export class ExcelFormatComponent {
                                            data: Array<any> = [];
                                            fileName: string = 'Example';

                                            constructor() { }
 
                                            jsonToExcel(): void {
                                                if(this.data.length === 0 || this.data === undefined || this.data === '' || this.data === null) {
                                                    console.log('No data found to download');
                                                }
                                                else {
                                                import("xlsx").then(xlsx => {
                                                    const worksheet = xlsx.utils.json_to_sheet(this.data);
                                                    let range = xlsx.utils.decode_range('!ref'); // The range can be used fot looping.
                                                    for(var R = 0; R <= this.data.length+1; R++) {
                                                        var addr = xlsx.utils.encode_cell({r:R, c:2}); // To formate second column all rows.
                                                        if(!worksheet[addr]) continue;
                                                        worksheet[addr].z = "#,##0.00000"; // the actual number format, "0.00" for two decimal places, "#,##0.00" for thousands, etc.
                                                    }
                                                    const workbook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
                                                    const excelBuffer: any = xlsx.write(workbook, { bookType: 'xlsx', type: 'array' });
                                                    this.saveAsExcelFile(excelBuffer, this.fileName);
                                                });
                                                console.log('Excel downloaded');
                                                }
                                            }

                                            // Helper function of jsonToExcel()
                                            saveAsExcelFile(buffer: any, fileName: string): void {
                                                let EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
                                                let EXCEL_EXTENSION = '.xlsx';
                                                const data: Blob = new Blob([buffer], {
                                                    type: EXCEL_TYPE
                                                });
                                                FileSaver.saveAs(data, fileName + EXCEL_EXTENSION);
                                            }

                                        }
                                    
                                

The "saveAs" function of file-saver library plays an important role here, it take your excelBuffer created by excel pre difine functions "json_to_sheet" and convert it to Excel file for you.
For excel formating, we are taking help of excle pre difine function "encode_cell", using this function we can custimeze the formate of cell as needed. "#,##0.00000" is a custom number format.
In the above example code we are formatting 2nd colomn of excel (All rows). You can formate anyrow or colomn by making changes in the for loop itration.

That's all you need to format excel file my friend!