Introduction
In my previous
article, I discussed how to perform an export to Excel in Kendo Grid for Angular 2. This blog is in continuation of that previous one. Here, I'm going to explain how to customize the column while exporting data to Excel in Kendo Grid.
Customizing the Exported Excel Columns in Kendo Grid
Let's customize the title of the column while exporting the Kendo Grid data to Excel. By default, the Grid exports its current column, that means it will copy the data from Grid to Excel as it is.
Figure 1
Click on "Export to Excel" button.
Figure 2
Data is now exported to Excel; you can notice that the title is same as it was in the Grid.
To customize the exported column which is different from current Grid columns, we need to include <kendo-excelexport-column> component inside the <kendo-grid-excel> component.
kendo-grid.component.ts
- import { Component } from '@angular/core';
- import { Observable } from 'rxjs/Rx';
- import { CategoriesService } from './kendo-grid.service';
- import { process } from '@progress/kendo-data-query';
- import { ExcelExportData } from '@progress/kendo-angular-excel-export';
- import { State } from '@progress/kendo-data-query';
- import {
- GridComponent,
- GridDataResult,
- DataStateChangeEvent
- } from '@progress/kendo-angular-grid';
- @Component({
- selector: 'app-kendo-grid',
- styleUrls: ['./kendo-grid.component.scss'],
- template: `
-
- <kendo-grid
- [data]="view | async"
- [pageSize]="state.take"
- [skip]="state.skip"
- [sort]="state.sort"
- [sortable]="true"
- [pageable]="true"
- [scrollable]="'none'"
- (dataStateChange)="dataStateChange($event)"
- >
- <ng-template kendoGridToolbarTemplate>
- <button type="button" kendoGridExcelCommand ><span class="k-icon k-i-file-excel"></span>Export to Excel</button>
- <button kendoGridPDFCommand><span class='k-icon k-i-file-pdf'></span>Export to PDF</button>
- </ng-template>
- <kendo-grid-column field="value" title="S.No" width="100"></kendo-grid-column>
- <kendo-grid-column field="text" title="Technology" width="200"></kendo-grid-column>
- <kendo-grid-excel fileName="Technology.xlsx" >
-
- <kendo-excelexport-column field="value" title="S.No">
- </kendo-excelexport-column>
- <kendo-excelexport-column field="text" title="Technology List">
- </kendo-excelexport-column>
-
- </kendo-grid-excel>
- <kendo-grid-pdf fileName="Technology.pdf" [allPages]="true" paperSize="A4" [repeatHeaders]="true" [landscape]="true">
- <kendo-grid-pdf-margin top="2cm" left="1cm" right="1cm" bottom="2cm"></kendo-grid-pdf-margin>
- <ng-template kendoGridPDFTemplate let-pageNum="pageNum" let-totalPages="totalPages">
- <div class="page-template">
- <div class="header">
- <div style="float: right">Page {{ pageNum }} of {{ totalPages }}</div>
- </div>
- <div class="footer">
- Page {{ pageNum }} of {{ totalPages }}
- </div>
- </div>
- </ng-template>
- </kendo-grid-pdf>
- </kendo-grid>
- `
- })
- export class KendoGridComponent {
- public view: Observable<GridDataResult>;
- public state: State = {
- skip: 0,
- take: 5
- };
-
- constructor(private service: CategoriesService) {
- this.view = service;
- this.service.query(this.state);
- }
-
- public dataStateChange(state: DataStateChangeEvent): void {
- this.state = state;
- this.service.query(state);
- }
-
-
-
-
- }
The column is now exported to the Excel as shown in below figure.
From the above image, you can notice that the exported column title is changed based on the title mentioned in the kendo-excelexport-column component.
I hope you have enjoyed this blog. Your valuable feedback, questions, and comments about this blog are always welcome.
Click
here to get the source code.