Introduction
Let’s explore exporting an Excel file using Exceljs using Angular service. Basically, there are two open-source libraries available to read/write an Excel in client-side applications include
-
ExcelJS
-
XLSX and XLSX-Style
But in this article, we will explore using ExcelJs. So, what is ExcelJS?. ExcelJS in typescript helps us to create, read/write and modify workbook styles in an Excel sheet, since it provides almost all the features compared with XLSX and XLSX-Style. I have provided the steps to export the Excel file in Angular service using ExcelJS. Now let’s get started.
Step 1
First, let’s create a new angular project using Angular CLI with the command below:
ng new angular-excel
cd angular-excel
Step 2
After creating a new Angular project, let’s add the below command to fetch references for external ExcelJS and file-saver dependencies
npm install @exceljs
npm install @file-saver
Step 3
The next step is to create a “services” directory/folder under “src”.
Create an excel.service.ts file using the below command:
ng g service excel
Now replace the below code in an excel.service.ts file
- import {
- Injectable
- } from '@angular/core';
- import {
- Workbook
- } from 'exceljs';
- import {
- DatePipe
- } from '@angular/common';
- import * as fs from 'file-saver';
- import * as moment from 'moment'
- @Injectable()
- export class ExcelService {
- constructor(private datePipe: DatePipe) {}
- async generateExcel(data, authfilter) {
- const header = ['User Name', 'Email', 'Phone Number', 'Address'];
- // Create workbook and worksheet
- const workbook = new Workbook();
- const worksheet = workbook.addWorksheet();
- // Cell Style : Fill and Header
- var TodayDate = new Date();
- let MMDDYY = moment(TodayDate).format('MMDDYY').toString();
- var FileName = "ExportuserData" + MMDDYY;
- const headerRow = worksheet.addRow(header);
- headerRow.eachCell((cell, number) => {
- cell.fill = {
- type: 'pattern',
- pattern: 'solid',
- fgColor: {
- argb: 'FFFFFFFF'
- },
- bgColor: {
- argb: 'FFFFFFFF'
- },
- };
- cell.font = {
- color: {
- argb: '00000000',
- },
- bold: true
- }
- cell.border = {
- top: {
- style: 'thin'
- },
- left: {
- style: 'thin'
- },
- bottom: {
- style: 'thin'
- },
- right: {
- style: 'thin'
- }
- };
- });
- data.forEach(d => {
- const row = worksheet.addRow(d);
- row.fill = {
- type: 'pattern',
- pattern: 'solid',
- fgColor: {
- argb: 'FFFFFFFF'
- }
- };
- row.font = {
- color: {
- argb: '00000000',
- },
- bold: false
- }
- row.eachCell((cell, number) => {
- cell.border = {
- top: {
- style: 'thin'
- },
- left: {
- style: 'thin'
- },
- bottom: {
- style: 'thin'
- },
- right: {
- style: 'thin'
- }
- };
- });
- });
- worksheet.getColumn(1).width = 30;
- worksheet.getColumn(2).width = 40;
- worksheet.getColumn(3).width = 20;
- worksheet.getColumn(4).width = 20;
- workbook.xlsx.writeBuffer().then((data: any) => {
- const blob = new Blob([data], {
- type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
- });
- fs.saveAs(blob, FileName + '.xlsx');
- });
- }
- }




vairam TamilpandianPosted Dec 27, 2022, 5:24 AM
Exceljs lib is able to support in asp.net mvc using angular applications? please anyone know help me
JosephsPosted Jun 13, 2022, 4:07 AM
Your code not working properly. where is this providerlist collection in ts file ?
RakshithPosted Jul 14, 2021, 3:11 PM
How to export Date object in d/m/yyyy format to excel sheet from angular using exceljs? When I tried exporting in d/m/yyyy format, it is getting converted to string and storing in excel. I want it to be stored as date field only on excel.
Shanaya SainiPosted Apr 10, 2021, 10:49 AM
Where you mentioned exportAsExcelFile this method ?
ajith muthukumarPosted Mar 27, 2021, 9:08 AM
Where you mentioned exportAsExcelFile this methode
shrinath shindePosted May 11, 2020, 7:30 PM
How do I add header and footer section which can be only seen during printing in excel sheet using angular 8?
sathis kumarPosted Apr 29, 2020, 7:23 AM
Can you share which version of angular and exceljs is used.
Haridhass ManiPosted Feb 5, 2020, 12:04 AM
Nice article bro ;)