Two important open source libraries available to read/write an excel in the client-side web applications are :
Both libraries are used to read, manipulate and write spreadsheet data and styles to XLSX and JSON.
Initially, I had used XLSX & XLSX-style libraries to create and format an excel from JSON but the adverse point of this open-source library is it doesn’t provide any feature to add an image in excel (add image feature is available in pro version), which was the big requirement for my excel.
Later on, I found ExcelJS, which is really easy to use, provide almost all the features of Excel, and the main thing is it also provide add image feature.
Export to Excel in Angular 8 using ExcelJS
Create a Angular 8 Project
Use below command to create a new Angular 6 project with Angular CLI.
Install ExcelJS Library
Important
The latest version of
We can fix this by upgrading the
If you find any issue in angular 8 with
Update tsconfig.js
ExcelJS is generally used for server side web applications in node. Here we want to use it in client side Angular application. for that we need to set
- Note
If you are using Angular 4 or 5 you need to use the following path in
In Linux, It is not able to find
Install file-saver
FileSaver.js is the solution to saving files on the client-side and is perfect for web apps that need to generate files, or for saving sensitive information that shouldn’t be sent to an external server.
It implements the
Install file-saver library using following command
Environment setup is done. Now lets start to build an excel.
We will create a separate service in our project called
Import ExcelJS and FileSaver
In
Create a separate method and data varibales.
In
In this method, I have created some data variables as below, We will export these data in excel sheet.
Note: You can pass data from the component as a parameter in
Create Workbook and Add Worksheet
Create a new workbook and add a new worksheet using
Add Row and format the fonts.
We will use
Add Image in worksheet
export const logoBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAFKCAMAAADcyF29AAABxVBMVEVHcEwJCAggFxEBAQE2KyQAAAA0LScAAAAAAAA1LysXEQ0EBAQFBAMDAwMLCQgGBQUFBAOEQhUHBwZjQSuScFoVFRZvNAx5NghcOyaudU8yMDBrNhOiViMZFhXEdD3Ef0+4ZzFISUdSJwliMA6BPA6lVR8CAgEDAgQPDhANDgsKCQoVFhPObi4SERS2VxcE.......";To use it in
generateExcel()
method, We need to import carlogo.js
in excel.service.ts
as below,Merge Cells
We can merge cells using
The coordinates calculated from the range will cover from the top-left of the first cell to the bottom right of the second.
Add Data with Header & Conditional Formatting
We will add a header row for car data records with a background color, as below,
ExcelJS directly doesn’t support conditional formatting, but we can achieve this functionality by assigning style based on required condition in angular, as below,
Note: Using
But in our example, we want to perform cell styling based on a conditional check, So we have saved the individual row using
Same way, you can add other rows like footer and additional information.
Export file using FileSaver
Now our workbook is ready to export. We can export it using
Final Code Review
Note : I have added
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment