Export To Excel And PDF In Kendo Grid For Angular

Introduction

This article tells you how to implement Export to excel and PDF functionality in Kendo Grid with remote data binding for Angular 2. Before going through this article, I strongly recommend you read my previous article, where I have explained how to implement kendo grid for Angular 2.

Please check on the list of Kendo UI component for Angular 2

Content

  1. Configuring Kendo Grid control with remote databinding for Angular 2.
  2. Export to Excel and PDF in Kendo Grid for Angular 2

Configuring Kendo Grid control with remote databinding for Angular 2

Before going to the initial setup of the project, just make sure that the latest versions of Node.js and NPM are installed in your system

Step 1

Install Angular-CLI package, open the command prompt and write the command given below to install Angular CLI package

npm install -g @angular-cli

Step 2

Go to the respective directory, where you need to save your project and run command given below in the command prompt to create a project.

ng new kendoGridAngular --style=scss

kendoGridAngular is our project name.

Step 3

Let’s create a separate folder structure for kendo grid component, run the below command

ng g component Kendo-Grid

The project structure as shown below, which is opened in Visual Studio.

 

Step 4

Run the below command to install the kendo components.

npm install --save @progress/kendo-angular-grid @progress/kendo-angular-dropdowns @progress/kendo-angular-inputs @progress/kendo-angular-dateinputs @progress/kendo-data-query @progress/kendo-angular-intl @progress/kendo-angular-l10n @progress/kendo-drawing @progress/kendo-angular-excel-export @angular/animations

Step 5

In this step we are going to create a service file, right click on the kendo-grid folder under app folder and add new typescript file, in my case I named it as kendo-grid.service.ts

kendo-grid.service.ts

I'm going to use the API response given below to construct my datasource for Kendo Grid which was created in my previous article.

API - http://github-ci-staging.azurewebsites.net/api/Technologies/TechnologiesList

Type - GET.

Testing the APIs, using POSTMAN.

 

Step 7

Open app.module.ts file and add the information of Kendo grid and service.

app.module.ts

  1. import { NgModule, enableProdMode } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  4. import { HttpModule } from '@angular/http';  
  5. import { GridModule} from '@progress/kendo-angular-grid';  
  6.   
  7. import { AppComponent } from './app.component';  
  8. import { KendoGridComponent } from './kendo-grid/kendo-grid.component' 
  9. import { CategoriesService } from './kendo-grid/kendo-grid.service';  
  10.   
  11. @NgModule({  
  12.     
  13.   declarations: [  
  14.     AppComponent,  
  15.     KendoGridComponent  
  16.   ],  
  17.   imports: [  
  18.       BrowserModule, BrowserAnimationsModule, GridModule, HttpModule  
  19.   ],  
  20.   providers: [CategoriesService],  
  21.   bootstrap: [AppComponent]  
  22. })  
  23. export class AppModule {  
  24.   
  25. }  

Step 8

Open kendo-grid.component.ts and write the code given below in it.

Kendo-grid.component.ts

  1. import { Component } from '@angular/core';  
  2. import { Observable } from 'rxjs/Rx';  
  3. import { CategoriesService } from './kendo-grid.service';  
  4. import { process } from '@progress/kendo-data-query';  
  5. import { ExcelExportData } from '@progress/kendo-angular-excel-export';  
  6. import { State } from '@progress/kendo-data-query';  
  7. import {  
  8.     GridDataResult,  
  9.     DataStateChangeEvent  
  10. } from '@progress/kendo-angular-grid';  
  11. @Component({  
  12.   selector: 'app-kendo-grid',  
  13.   styleUrls: ['./kendo-grid.component.scss'],  
  14.    template: `  
  15.        <kendo-grid  
  16.           [data]="view | async"  
  17.           [pageSize]="state.take"  
  18.           [skip]="state.skip"  
  19.           [sort]="state.sort"  
  20.           [sortable]="true"  
  21.           [pageable]="true"  
  22.           [scrollable]="'none'"  
  23.           (dataStateChange)="dataStateChange($event)"  
  24.         >  
  25.         
  26.         <kendo-grid-column field="value" title="S.No" width="100"></kendo-grid-column>  
  27.         <kendo-grid-column field="text" title="Technology" width="200"></kendo-grid-column>  
  28.   
  29.       </kendo-grid>  
  30.     `  
  31. })  
  32. export class KendoGridComponent {  
  33.     public view: Observable<GridDataResult>;  
  34.     public state: State = {  
  35.         skip: 0,  
  36.         take: 5  
  37.     };  
  38.   
  39.     constructor(private service: CategoriesService) {  
  40.         this.view = service;  
  41.         this.service.query(this.state);  
  42.     }  
  43.   
  44.     public dataStateChange(state: DataStateChangeEvent): void {  
  45.         this.state = state;  
  46.         this.service.query(state);  
  47.     }  
  48.   
  49.   
  50. }  

Step 9

Configure the theme

In this step, we are going to install Kendo default theme into our Application. Use the command given below to install the theme

npm install --save @progress/kendo-theme-default

Open the style.scss and write the code given below in it.

@import "~@progress/kendo-theme-default/scss/all"

The code given above is used to integrate Kendo's default theme into our Application.

Step 10

Build the Application

Use the command given below to build and run the Application.

ng-serve

Result

Export to Excel and PDF in Kendo Grid for Angular 2

1. Export to Excel

To implement export to excel functionality, we need to include Kendo-grid-excel component in the grid. 

Open app.module.ts, and write the below code

  1. import { NgModule, enableProdMode } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  4. import { HttpModule } from '@angular/http';  
  5. import { GridModule, ExcelModule } from '@progress/kendo-angular-grid';  
  6.   
  7. import { AppComponent } from './app.component';  
  8. import { KendoGridComponent } from './kendo-grid/kendo-grid.component';  
  9. import { CategoriesService } from './kendo-grid/kendo-grid.service';  
  10.   
  11. @NgModule({  
  12.     
  13.   declarations: [  
  14.     AppComponent,  
  15.     KendoGridComponent  
  16.   ],  
  17.   imports: [  
  18.       BrowserModule, BrowserAnimationsModule, GridModule, HttpModule, ExcelModule  
  19.   ],  
  20.   providers: [CategoriesService],  
  21.   bootstrap: [AppComponent]  
  22. })  
  23. export class AppModule {  
  24.   
  25. }  

From the above code, it is clear the Excel module is imported from kendo grid to add the export to excel feature in grid

Open kendo-grid.component.ts, and write the below code

  1. import { Component } from '@angular/core';  
  2. import { Observable } from 'rxjs/Rx';  
  3. import { CategoriesService } from './kendo-grid.service';  
  4. import { process } from '@progress/kendo-data-query';  
  5. import { ExcelExportData } from '@progress/kendo-angular-excel-export';  
  6. import { State } from '@progress/kendo-data-query';  
  7. import {  
  8.     GridDataResult,  
  9.     DataStateChangeEvent  
  10. } from '@progress/kendo-angular-grid';  
  11. @Component({  
  12.   selector: 'app-kendo-grid',  
  13.   styleUrls: ['./kendo-grid.component.scss'],  
  14.    template: `  
  15.        <kendo-grid  
  16.           [data]="view | async"  
  17.           [pageSize]="state.take"  
  18.           [skip]="state.skip"  
  19.           [sort]="state.sort"  
  20.           [sortable]="true"  
  21.           [pageable]="true"  
  22.           [scrollable]="'none'"  
  23.           (dataStateChange)="dataStateChange($event)"  
  24.         >  
  25.         <ng-template kendoGridToolbarTemplate>  
  26.                 <button type="button" kendoGridExcelCommand ><span class="k-icon k-i-file-excel"></span>Export to Excel</button>  
  27.             </ng-template>  
  28.         <kendo-grid-column field="value" title="S.No" width="100"></kendo-grid-column>  
  29.         <kendo-grid-column field="text" title="Technology" width="200"></kendo-grid-column>  
  30.     <kendo-grid-excel fileName="Technology.xlsx" ></kendo-grid-excel>  
  31.         
  32.       </kendo-grid>  
  33.     `  
  34. })  
  35. export class KendoGridComponent {  
  36.     public view: Observable<GridDataResult>;  
  37.     public state: State = {  
  38.         skip: 0,  
  39.         take: 5  
  40.     };  
  41.   
  42.     constructor(private service: CategoriesService) {  
  43.         this.view = service;  
  44.         this.service.query(this.state);  
  45.     }  
  46.   
  47.     public dataStateChange(state: DataStateChangeEvent): void {  
  48.         this.state = state;  
  49.         this.service.query(state);  
  50.     }  
  51.   
  52.   
  53. }  

KendoGridToolbar template is used to display the excel to export button in grid and we need to add Kendo-grid-excel component in the grid to enable the export to excel feature

Result


Click on export to excel to download the data in excel format 

2. Export to PDF

To implement export to excel functionality, we need to include Kendo-grid-pdf component in the grid.  

Open app.module.ts, and write the below code 

  1. import { NgModule, enableProdMode } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  4. import { HttpModule } from '@angular/http';  
  5. import { GridModule, ExcelModule, PDFModule } from '@progress/kendo-angular-grid';  
  6.   
  7. import { AppComponent } from './app.component';  
  8. import { KendoGridComponent } from './kendo-grid/kendo-grid.component';  
  9. import { CategoriesService } from './kendo-grid/kendo-grid.service';  
  10.   
  11. @NgModule({  
  12.     
  13.   declarations: [  
  14.     AppComponent,  
  15.     KendoGridComponent  
  16.   ],  
  17.   imports: [  
  18.       BrowserModule, BrowserAnimationsModule, GridModule, HttpModule, ExcelModule, PDFModule  
  19.   ],  
  20.   providers: [CategoriesService],  
  21.   bootstrap: [AppComponent]  
  22. })  
  23. export class AppModule {  
  24.   
  25. }  
 From the above code, it is clear the PDF module is imported from kendo grid to add the export to PDF feature in grid

Open kendo-grid.component.ts, and write the below code

  1. import { Component } from '@angular/core';  
  2. import { Observable } from 'rxjs/Rx';  
  3. import { CategoriesService } from './kendo-grid.service';  
  4. import { process } from '@progress/kendo-data-query';  
  5. import { ExcelExportData } from '@progress/kendo-angular-excel-export';  
  6. import { State } from '@progress/kendo-data-query';  
  7. import {  
  8.     GridDataResult,  
  9.     DataStateChangeEvent  
  10. } from '@progress/kendo-angular-grid';  
  11. @Component({  
  12.   selector: 'app-kendo-grid',  
  13.   styleUrls: ['./kendo-grid.component.scss'],  
  14.    template: `  
  15.        <kendo-grid  
  16.           [data]="view | async"  
  17.           [pageSize]="state.take"  
  18.           [skip]="state.skip"  
  19.           [sort]="state.sort"  
  20.           [sortable]="true"  
  21.           [pageable]="true"  
  22.           [scrollable]="'none'"  
  23.           (dataStateChange)="dataStateChange($event)"  
  24.         >  
  25.         <ng-template kendoGridToolbarTemplate>  
  26.                 <button type="button" kendoGridExcelCommand ><span class="k-icon k-i-file-excel"></span>Export to Excel</button>  
  27.                <button kendoGridPDFCommand><span class='k-icon k-i-file-pdf'></span>Export to PDF</button>  
  28.             </ng-template>  
  29.         <kendo-grid-column field="value" title="S.No" width="100"></kendo-grid-column>  
  30.         <kendo-grid-column field="text" title="Technology" width="200"></kendo-grid-column>  
  31.     <kendo-grid-excel fileName="Technology.xlsx" ></kendo-grid-excel>  
  32.       <kendo-grid-pdf fileName="Technology.pdf" [allPages]="true" paperSize="A4" [repeatHeaders]="true" [landscape]="true">  
  33.                 <kendo-grid-pdf-margin top="2cm" left="1cm" right="1cm" bottom="2cm"></kendo-grid-pdf-margin>  
  34.                 <ng-template kendoGridPDFTemplate let-pageNum="pageNum" let-totalPages="totalPages">  
  35.                  <div class="page-template">  
  36.                     <div class="header">  
  37.                       <div style="float: right">Page {{ pageNum }} of {{ totalPages }}</div>  
  38.                     </div>  
  39.                     <div class="footer">  
  40.                       Page {{ pageNum }} of {{ totalPages }}  
  41.                     </div>  
  42.                   </div>  
  43.                 </ng-template>  
  44.             </kendo-grid-pdf>  
  45.       </kendo-grid>  
  46.     `  
  47. })  
  48. export class KendoGridComponent {  
  49.     public view: Observable<GridDataResult>;  
  50.     public state: State = {  
  51.         skip: 0,  
  52.         take: 5  
  53.     };  
  54.   
  55.     constructor(private service: CategoriesService) {  
  56.         this.view = service;  
  57.         this.service.query(this.state);  
  58.     }  
  59.   
  60.     public dataStateChange(state: DataStateChangeEvent): void {  
  61.         this.state = state;  
  62.         this.service.query(state);  
  63.     }  
  64.   
  65.   
  66. }  

KendoGridToolbar template is used to display the excel to PDF button in grid and we need to add Kendo-grid-pdf component in the grid to enable the export to pdf feature. 

 Result


Click on Export to PDF button to download the data in PDF format

 
Conclusion

In this article we have seen a basic implementation of export to excel and pdf functionality in kendo grid, will see more custom features in the functionality of Kendo grid for Angular 2 in my future articles

I hope, you have enjoyed this article. Your valuable feedback, questions or comments about this article are always welcome.

References
  1. http://www.telerik.com/kendo-angular-ui/getting-started
  2. http://www.telerik.com/kendo-angular-ui/components/grid/excel-export/ 


Similar Articles