# Exporting Selected Grid Rows while Preserving Data Operations

## Environment

<table>
    <tbody>
	    <tr>
	    	<td>Product</td>
	    	<td>Progress® Kendo UI for Angular Grid</td>
	    </tr>
    </tbody>
</table>

## Description

How can I export selected Grid rows to Excel and PDF while maintaining the current filtering, sorting, and grouping? When rows are selected across multiple pages, I want to export all selected rows with the current data operations applied.

This Knowledge Base article also answers the following questions:

* How to export only selected Grid rows with filtering, sorting, and grouping applied?
* Why do exported selected rows not reflect the Grid's data operations?
* How to export all data with filtering, sorting, and grouping when no rows are selected?

## Solution

To export selected rows across pages while preserving the applied data operations, use the [`kendoGridSelectBy`](https://www.telerik.com/kendo-angular-ui/components/grid/api/selectiondirective) directive to persist selection and the [`process`](https://www.telerik.com/kendo-angular-ui/components/data-query/api/process) method to apply the operations during export.

<TabStrip>
<TabStripTab title="Excel Export">

To export selected rows to Excel with data operations preserved:

1. Use the [`kendoGridSelectBy`](https://www.telerik.com/kendo-angular-ui/components/grid/api/selectiondirective) directive to store the entire `dataItem` in the [`selectedKeys`](https://www.telerik.com/kendo-angular-ui/components/grid/api/selectiondirective#selectedkeys) collection and preserve selection across pages.

   ```TS
   public selectedKeys: Product[] = [];

   public selectBy = (args: RowArgs) => {
     return args.dataItem;
   };
   ```

   ```HTML
   <kendo-grid
     [kendoGridSelectBy]="selectBy"
     [(selectedKeys)]="selectedKeys"
   >
   </kendo-grid>
   ```

2. Create a helper method that determines which data to export (selected rows or all data) and applies the current data operations using the [`process`](https://www.telerik.com/kendo-angular-ui/components/data-query/api/process) method.

   ```TS
   public gridState: State = {
     skip: 0,
     take: 10,
     sort: [],
     group: [],
     filter: undefined
   };

   private getProcessedExportData(): Product[] {
     const dataToExport = this.selectedKeys.length > 0 ? this.selectedKeys : products;
     const allDataState = { ...this.gridState, take: products.length };
     return process(dataToExport, allDataState).data;
   }
   ```

   To export all pages when there are no selected rows, override the `take` field passed in the `process` method and set it to the total number of records in the Grid.

3. Use the [`fetchData`](https://www.telerik.com/kendo-angular-ui/components/grid/api/excelcomponent#fetchdata) callback to call the helper method and return the processed data.

   ```TS
   public getExcelData = (): ExcelExportData => {
     return {
       data: this.getProcessedExportData(),
       group: this.gridState.group,
     };
   };
   ```

   ```HTML
   <kendo-grid-excel [fetchData]="getExcelData" fileName="Products.xlsx">
   </kendo-grid-excel>
   ```

</TabStripTab>
<TabStripTab title="PDF Export">

To export selected rows to PDF with data operations preserved:

1. Use the [`kendoGridSelectBy`](https://www.telerik.com/kendo-angular-ui/components/grid/api/selectiondirective) directive to store the entire `dataItem` in the [`selectedKeys`](https://www.telerik.com/kendo-angular-ui/components/grid/api/selectiondirective#selectedkeys) collection and preserve selection across pages.

   ```TS
   public selectedKeys: Product[] = [];

   public selectBy = (args: RowArgs) => {
     return args.dataItem;
   };
   ```

   ```HTML
   <kendo-grid
     [kendoGridSelectBy]="selectBy"
     [(selectedKeys)]="selectedKeys"
   >
   </kendo-grid>
   ```

2. Create a helper method that determines which data to export (selected rows or all data) and applies the current data operations using the [`process`](https://www.telerik.com/kendo-angular-ui/components/data-query/api/process) method.

   ```TS
   public gridState: State = {
     skip: 0,
     take: 10,
     sort: [],
     group: [],
     filter: undefined
   };

   private getProcessedExportData(): Product[] {
     const dataToExport = this.selectedKeys.length > 0 ? this.selectedKeys : products;
     const allDataState = { ...this.gridState, take: products.length };
     return process(dataToExport, allDataState).data;
   }
   ```

   To export all pages when there are no selected rows, override the `take` field passed in the `process` method and set it to the total number of records in the Grid.

3. Since the PDF export captures the content that is currently visible on the page, create a second Grid off-screen and bind its data to a getter, which calls the helper method to return the processed data.

   ```HTML
   <div style="position: fixed; top: 0; left: -10000px; width: 800px;">
     <kendo-grid [data]="pdfExportData" #pdfGrid>
       <kendo-grid-column field="ProductName" title="Product Name"></kendo-grid-column>
       ...
     </kendo-grid>
   </div>
   ```

   ```TS
   public get pdfExportData(): Product[] {
     return this.getProcessedExportData();
   }
   ```

4. Use the [`saveAsPDF`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#saveaspdf) method to export the hidden Grid to PDF.

   ```TS
   @ViewChild('pdfGrid') public pdfGrid!: GridComponent;

   public onPdfExport(): void {
     this.pdfGrid.saveAsPDF();
   }
   ```

</TabStripTab>
</TabStrip>

The following example demonstrates the full implementation for both Excel and PDF export.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/knowledge-base/grid-export-selected-rows-maintain-state/source.txt)

## See Also

* [Export Selected Rows to Excel](https://www.telerik.com/kendo-angular-ui/components/knowledge-base/export-selected-rows-excel)
* [Export Selected Rows to PDF](https://www.telerik.com/kendo-angular-ui/components/knowledge-base/export-selected-rows-pdf)
* [Exporting Filtered Data to Excel](https://www.telerik.com/kendo-angular-ui/components/knowledge-base/grid-export-filtered-data-excel)
* [Angular Data Grid Excel Export](https://www.telerik.com/kendo-angular-ui/components/grid/export/excel-export)
* [Grid PDF Export](https://www.telerik.com/kendo-angular-ui/components/grid/export/pdf-export)
* [Grid Selection Basics](https://www.telerik.com/kendo-angular-ui/components/grid/selection)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent)
