When we develop a simple application or complex enterprise application using any technology, The end-user always needs the report data in an Excel file. Why because most people rely on excel file. I worked with various Angular Enterprise applications. In all applications, the customer asked us to give a downloadable excel file using various filters.
In this tutorial, you will learn, how to download an excel file using Angular. I am going to show a demo of excel file download using ExcelJS and File Saver plugin. If you are an Angular developer and looking for export data to excel, then this post will help you more. We will see them one by one.
Step 1. Create New Angular Project
Create a new Angular project using the below command.
ng new angular-excel-example
Once the initial setup is done, then open the project folder using the VS code editor.
Step 2. Install ExcelJS Plugin
ExcelJS plugin is used to read and write excel file. It is rich in functionality. You can style rows and columns. Even you can add images to excel files. That’s why many people using this library.
Install ExcelJS using the below command.
npm install exceljs --save
Step 3. Install File Saver
FileSaver.js is the solution to saving files on the client-side and is perfect for web apps that generate files on the client.
Install file saver plugin using the below command.
npm install file-saver --save
Step 4. Create Button With Click Event
Create a button on the app.component.html file with click event.
<div> <button class="centre btn mc" (click)="downloadExcel()">Download</button> </div>
Step 5. Import ExcelJS and File Saver Plugin
Open the app.component.ts file and import the ExcelJS and File Saver plugin using the below code.
import { Workbook } from 'exceljs'; import * as fs from 'file-saver';
Step 6. Create a Downloadable Excel File
Here I am going to explain, each piece of code in a step by step manner.
- First, we need data in the JSON format. You can create dummy data like me for testing purposes or you can get the data from the server.
Here I creating a variable with some name and age data.
json_data=[{ "name": "Raja", "age": 20 }, { "name": "Mano", "age": 40 }, { "name": "Tom", "age": 40 }, { "name": "Devi", "age": 40 }, { "name": "Mango", "age": 40 } ]
2. Next, create a workbook excel workbook using
//create new excel work book let workbook = new Workbook();
3. From the workbook, we can create a new sheet using the addWorksheet() function. Give a sheet name here
//add name to sheet let worksheet = workbook.addWorksheet("Employee Data");
4. Add column header using the addRow() function. In the JSON data, I have only two columns. That’s why I creating the only two columns.
//add column name let header=["Name","Age"] let headerRow = worksheet.addRow(header);
5. Now add the JSON data to the worksheet using the for loop.
for (let x1 of this.json_data) { let x2=Object.keys(x1); let temp=[] for(let y of x2) { temp.push(x1[y]) } worksheet.addRow(temp) }
6. Set the file name and call the write function to create a downloadable excel file.
//set downloadable file name let fname="Emp Data Sep 2020" //add data and file name and download workbook.xlsx.writeBuffer().then((data) => { let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); fs.saveAs(blob, fname+'-'+new Date().valueOf()+'.xlsx'); });
Comments
Post a Comment