Data Export In Angular Using Ignite-UI


Export to Excel or CSV file is a very common need in every web project which is mostly used for reporting the purposes but to export, the data in the Excel from the data source takes a huge code. Infragistics provides a very simple way to achieve this in Angular which takes either a data source or IgxGrid data grid as source and converts it into Excel, CSV, or TSV file very easily. IgxGrid data grid is a vast concept so in this article, we will use the data source to export the file.

What we will cover in this article

In this article, we will cover the following features,

  • Export Data from the data source to CSV File
  • Export Data from the data source to TSV File
  • Export Data from the data source to Excel File
  • Filter Data before Export

Let's get started,

In case you have not set up the Ignite-UI for Angular, Click here for proper documentation provided by the Ignite. You can use either the Ignite-CLI or Angular-CLI. I prefer to go through the Angular-CLI manual process which explores that how the Ignite UI works.

After the setup, the Ignite UI for Angular, let us open the VS Code opened in the terminal window and type the following command to make a new Project.

ng new Export-Demo

Listing 1

Open the newly created project folder in VS Code. Now, go to the app.module.ts file and add the following code in the file. Refer to listing 2.

  1. import { IgxCsvExporterService, IgxExcelExporterService} from 'igniteui-angular';  
  2. import { FormsModule } from '@angular/forms';  
  4. imports: [  
  5.       BrowserModule,  
  6.            FormsModule  
  7.         ],  
  8.         providers: [IgxCsvExporterService, IgxExcelExporterService]  

Listing 2

Let’s open the app.component.html file and paste the code shown in Listing 3.

In this HTML, we are creating UI for collecting employee information. The user can insert the details of employee and click on add which will bind the inserted employee details in the grid.

  1. <div class="container">  
  2.         <div class="row">  
  3.             <div class="col-md-6">  
  4.                 <h2>  
  5.                     Add New Employee  
  6.                 </h2>  
  7.             </div>  
  8.         </div>  
  9.         <div class="row">  
  10.             <div class="col-md-3">  
  11.                 Employee Name  
  12.             </div>  
  13.             <div class="col-md-3">  
  14.                 Employee Address  
  15.             </div>  
  16.             <div class="col-md-3">  
  17.                 Employee Department  
  18.             </div>  
  19.             <div class="col-md-3">  
  20.                 Employee Salary  
  21.             </div>  
  22.         </div>  
  23.         <div class="row">  
  24.             <div class="col-md-3">  
  25.                 <input type="text" [(ngModel)]="" class="form-control">  
  26.             </div>  
  27.             <div class="col-md-3">  
  28.                 <input type="text" [(ngModel)]="model.address" class="form-control">  
  29.             </div>  
  30.             <div class="col-md-3">  
  31.                 <input type="text" [(ngModel)]="model.deptName" class="form-control">  
  32.             </div>  
  33.             <div class="col-md-3">  
  34.                 <input type="text" [(ngModel)]="model.salary" class="form-control">  
  35.             </div>  
  36.         </div>  
  37.         <div class="row">  
  38.             <div class="col-md-3">  
  39.                 <input type="button" (click)="AddEmployee(model)" value="Save" class="btn btn-success">  
  40.             </div>  
  41. </div>  
  42. <div class="row" style="margin-top: 15px" *ngIf="empList.length > 0">  
  43.         <div class="col-md-12">  
  44.             <h3>  
  45.                 Employee List  
  46.             </h3>  
  47.         </div>  
  48.         <div class="col-md-12">  
  49.             <table class="table table-bordered">  
  50.                 <tr>  
  51.                     <th>  
  52.                         Employee Name  
  53.                     </th>  
  54.                     <th>  
  55.                         Employee Address  
  56.                     </th>  
  57.                     <th>  
  58.                         Employee Department  
  59.                     </th>  
  60.                     <th>  
  61.                         Employee Salary  
  62.                     </th>  
  63.                 </tr>  
  64.                 <tr *ngFor="let emp of empList">  
  65.                     <td>  
  66.                         {{}}  
  67.                     </td>  
  68.                     <td>  
  69.                         {{emp.address}}  
  70.                     </td>  
  71.                     <td>  
  72.                         {{emp.deptName}}  
  73.                     </td>  
  74.                     <td>  
  75.                         {{emp.salary}}  
  76.                     </td>  
  77.                 </tr>  
  78.             </table>  
  79.      </div>  
  80. </div>  

Listing 3

Let’s create a folder and add one model named Employee.ts in our project which help us to get data from UI to backend. Refer to Listing 4.

  1. export class Employee {  
  2.        name: string;  
  3.        address: string;  
  4.        deptName: string;  
  5.        salary: number;  {

Listing 4

Open app.component.ts file and add the following code shown in Listing 5, First import the employee model on the top of the file “import { Employee } from '../models/Employee'” and add two variable to hold the values. Refer to Listing 5.

  1. import { Component } from '@angular/core';  
  2. import { Employee } from '../models/Employee';  
  4. export class AppComponent {  
  5.         title = 'app';  
  7.         model: Employee = new Employee();  
  8.         empList: Employee[] = [];  
  10.         AddEmployee(obj: Employee): void {  
  11.             this.empList.push(obj);  
  12.             this.model = new Employee();  
  13.         }  
  14. }  

Listing 5

Now, go to the VS Code terminal and hit the command shown in listing 6 to run the app. The final output will be like figure 1.

ng serve --o

Listing 6

Data Export In Angular Using Ignite-UI 
Figure 1

As listing 2 shows we already import the required services for import feature in app.module.ts.

Now, let’s add three button and 3 function for Export to CSV, Export to TSV and Export to Excel with corresponding function in app.component.ts file. Refer to Lsiting 7.

  1.  <div class="col-md-12">  
  2.         <input type="button" value="Export To CSV" (click)="ExportToCSV()" class="btn btn-primary">         
  3.         <input type="button" value="Export To TSV" (click)="ExportToTSV()" class="btn btn-warning">         
  4.         <input type="button" value="Export To Excel" (click)="ExportToExcel()" class="btn btn-danger">  
  5. </div>  

Listing 7

Now, let’s go to the app.compont.ts file and first import the required services and inject in constructor. Then add three function to export and their implementation. Refer to Listing 8. In all these three function we are passing the empList object to an encapsulated exportdatafunction.

  1. import { Employee } from '../models/Employee';  
  2. import { CsvFileTypes, IgxCsvExporterOptions, IgxCsvExporterService, IgxExcelExporterOptions, IgxExcelExporterService } from "igniteui-angular";  
  4. export class AppComponent {  
  5.         title = 'app';  
  7.         model: Employee = new Employee();  
  8.         empList: Employee[] = [];  
  10.         constructor(private csvExportService: IgxCsvExporterService, private excelExportService: IgxExcelExporterService) {  
  12.         }  
  14.         AddEmployee(obj: Employee): void {  
  15.             this.empList.push(obj);  
  16.             this.model = new Employee();  
  17.         }  
  19.         ExportToCSV() {  
  20.             const opt: IgxCsvExporterOptions = new IgxCsvExporterOptions("CSVExportFileFromData", CsvFileTypes.CSV);  
  21.             this.csvExportService.exportData(this.empList, opt);  
  22.         }  
  24.         ExportToTSV() {  
  25.             const opt: IgxCsvExporterOptions = new IgxCsvExporterOptions("CSVExportFileFromData", CsvFileTypes.TSV);  
  26.             this.csvExportService.exportData(this.empList, opt);  
  27.         }  
  29.         ExportToExcel() {  
  30.             this.excelExportService.exportData(this.empList, new IgxExcelExporterOptions("ExportFileFromData"));  
  31.         }  
  32. }  

Listing 8

Save the code and go back to the browser. You will see three different button with different mode of export functionality. Click on buttons and file get is download. Refer to Figure 2.

Data Export In Angular Using Ignite-UI
Figure 2


Filter data before export

We can also filter the source before exporting info file, i.e., Excel, CSV or TSV. We can either filter the row or we can exclude a whole column to export. This might be useful when we need to remove the irrelevant data from our report. Let’s modify the previous examples and create two more control. First control decide which column to exclude from excel report and second control decide which row to remove by employee name. For that let’s add one dropdown for column name which has static values and one textbox for employee name. Both of them have a ngModel property which can later pass two excel function. Refer to Listing 9.

  1.  <div class="col-md-12">  
  2.         <div class="row">  
  3.             <div class="col-md-3">  
  4.                 Column to hide in Excel only  
  6.                 <select [(ngModel)]="ColumnToHide" class="form-control">  
  7.                     <option value="Select">  
  8.                         Select  
  9.                     </option>  
  10.                     <option value="name">  
  11.                         Name  
  12.                     </option>  
  13.                     <option value="address">  
  14.                         Address  
  15.                     </option>  
  16.                     <option value="deptName">  
  17.                         Department Name  
  18.                     </option>  
  19.                     <option value="salary">  
  20.                         Salary  
  21.                     </option>  
  22.                 </select>  
  23.             </div>  
  24.             <div class="col-md-3">  
  25.                 Employee Name to filter  
  26.                 <input type="text" class="form-control" [(ngModel)]="EmpNameToRemove">  
  27.             </div>  
  28.         </div>  
  29. </div>  
  30. <input type="button" value="Export To Excel" (click)="ExportToExcel(ColumnToHide, EmpNameToRemove )" class="btn btn-danger">  

Listing 9

Let’s modify the export function in app.component.ts. There are two function which can be used to filter the data before exporting to excel i.e. onRowExport and onColumnExport. Both of them are event emitter type which which returns IRowExportingEventArgs and IColumnExportingEventArgs. To use these function let’s subscribe them and set the cancel property to true. Refer to listing 10.

  1. ExportToExcel(col: string, empName: string) {  
  2.    this.excelExportService.onRowExport.subscribe((args: IRowExportingEventArgs) => {  
  3.      if ( == empName) // this will check the row before exporting and where      name field is same as 'Jatin Saini', it will go inside that block  
  4.        args.cancel = true// this set flag to true (not to export)  
  5.    });  
  7.    this.excelExportService.onColumnExport.subscribe((args: IColumnExportingEventArgs) => {  
  8.      if (args.header == col) // this will check the header name and if header name is 'name' it will      come inside   
  9.        args.cancel = true// this set flag to true (not to export)  
  10.    });  
  12.    this.excelExportService.exportData(this.empList, new     IgxExcelExporterOptions("ExportFileFromData"));  
  13.  }  

Listing 10

Now, if I save the code and go back to the browser and add the random data. You will see column name to skip and employee name to skip the whole row. When you click on export to excel button these column/row will not be exported. Refer to Figure 3.

Data Export In Angular Using Ignite-UI 
Figure 3


Infragistics provides several features in this Exporter that can change the height of a cell or you can change the width of the column, pin the column, set the column order etc. Infragistics concentrate on how they can provide maximum features of control with the simple and minimum implementation of control on the project.