# Angular Data Grid CSV Export

Export your Grid data to CSV format for seamless integration with spreadsheet applications and data processing tools.

The Grid enables you to create CSV files with custom delimiters, headers, and encoding options. Export all data or just what's visible, configure field separators, and customize every aspect of the generated file.

## Getting Started

Import the `KENDO_GRID_CSV_EXPORT` utility array and nest the [`kendo-grid-csv`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent) component inside `kendo-grid` tags.
Trigger the export using the [`kendoGridCSVCommand`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcommanddirective) directive, [`kendoGridCSVTool`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/toolbar/grid-toolbar) toolbar tool, or [`saveAsCSV`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#saveascsv) method.

```html
<kendo-grid>
    <kendo-grid-csv></kendo-grid-csv>
</kendo-grid>
```

```ts
import { KENDO_GRID, KENDO_GRID_CSV_EXPORT } from '@progress/kendo-angular-grid';

@Component({
    ...
    imports: [KENDO_GRID, KENDO_GRID_CSV_EXPORT],
})
export class AppComponent { }
```

The following example demonstrates the CSV export functionality of the Grid.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/basic/source.txt)

## Controlling the Exported Data

By default, the Grid exports only the currently visible data—the current page when paging is enabled, or a batch of rows when virtual scrolling is used. However, you often need to export a different data set than what appears on screen, such as all records across all pages or only selected rows.

The [`fetchData`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#fetchdata) callback function gives you complete control over which data appears in the CSV file. This function receives the current Grid data and lets you transform or replace it before export.

### Exporting Different Data Sets

You can use the [`fetchData`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#fetchdata) callback to export all data of the Grid or various subsets based on your specific requirements.

```html
<kendo-grid>
    <kendo-grid-csv [fetchData]="customData"></kendo-grid-csv>
</kendo-grid>
```

```ts
public customData = (): any[] => {
    // TODO: return the records that the CSV export must include.
    return [...];
};
```

The following example demonstrates how to export all data when the Grid is paginated. Instead of exporting only the visible page, the export includes all records. The same approach works when virtual scrolling is enabled.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/all-pages/source.txt)

The `fetchData` function must return an array of data objects or a [`DataResult`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/dataresult) object. The function receives the Grid component and lets you access its data and configuration. This enables various scenarios:

* Export all data—Retrieve the complete data set regardless of paging or virtual scrolling.
* Export selected rows only—Export only the rows that users have selected in the Grid.
* Export filtered data—Export data based on specific filtering criteria.
* Export processed data—Apply custom sorting, filtering, or transformations before export.

### Exporting Data from Remote Sources

You can use the [`fetchData`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#fetchdata) callback to export data from remote servers or APIs. This is especially useful when the Grid displays paginated or virtualized data, but you need to export the complete data set.

The callback function supports asynchronous data loading by accepting [`Observable`](https://rxjs.dev/guide/observable) or [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) return types, allowing you to load data from remote sources before the export begins.

The following example demonstrates how to export all data from a remote source. The Grid displays paginated data, but clicking the Export button fetches the complete data set from the server and exports it to CSV.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/all-pages-observable/source.txt)

## Exporting Grouped Data

The Grid supports exporting grouped data to CSV format. When the Grid is grouped, the exported CSV file reflects the group structure, including group headers and aggregated values. You can customize the appearance of group headers and values using the [`groupHeaderFormatter`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#groupheaderformatter) and [`groupValueFormatter`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#groupvalueformatter) properties.

```html
<kendo-grid>
    <kendo-grid-csv
        [groupHeaderFormatter]="groupHeaderFormatter"
        [groupValueFormatter]="groupValueFormatter"
    ></kendo-grid-csv>
</kendo-grid>
```

```typescript
public groupHeaderFormatter = (displayNames: string[]): string => {
    // Use a separator that preserves the hierarchy of grouped fields.
    return displayNames.join(' > ');
};

public groupValueFormatter = (values: any[], groupNames: string[]): string => {
    // Pair each group value with its corresponding field name.
    return values.map((value, index) => `${groupNames[index]}: ${value}`).join(', ');
};
```

By default, the Grid uses a pipe character `|` to separate group field names and group values. You can customize these formatters to create more meaningful group representations in your CSV files.

The following example demonstrates how to export grouped data with custom group header and value formatting.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/grouped-data/source.txt)

## Exporting Selected Data

You can export only the Grid rows selected by users instead of exporting all data. This is useful when users want to export specific records they have chosen for analysis or reporting purposes.

Use the [`kendoGridSelectBy`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/selectiondirective) directive combined with the [`selectedKeys`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/selectiondirective#selectedkeys) property to track selected rows, and the `fetchData` callback to filter and export only the selected items. The selection directive stores the selected items in the `selectedKeys` collection, which you can use to identify which rows to include in the export.

The following example demonstrates how to export selected rows to CSV. Users can select specific rows using checkboxes, and only the selected data will be included in the exported file.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/export-selected/source.txt)

## CSV Export Options

The CSV export component provides several properties to customize the generated CSV file according to your needs. You can adjust the delimiter, line separators, encoding options, column headers, and more.

The following example demonstrates how to customize various CSV export options.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/custom-options/source.txt)

### Delimiter and Line Separators

The [`delimiter`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#delimiter) property sets the character used to separate values in each row. By default, the Grid uses a comma (`,`), but you can change it to semicolons, tabs, or other characters based on your regional settings or application requirements.

The [`lineSeparator`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#lineseparator) property controls how rows are separated. The default value is `\r\n` (Windows line ending), but you can customize it for different operating systems or specific requirements.

```html
<kendo-grid>
    <kendo-grid-csv delimiter=";" lineSeparator="\n"></kendo-grid-csv>
</kendo-grid>
```

### UTF-8 BOM

The [`includeUTF8BOM`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#includeutf8bom) property, when set to `true`, adds a UTF-8 Byte Order Mark (BOM) at the beginning of the file. This helps applications like Microsoft Excel correctly recognize UTF-8 encoding and display special characters properly.

```html
<kendo-grid>
    <kendo-grid-csv [includeUTF8BOM]="true"></kendo-grid-csv>
</kendo-grid>
```

### Custom Column Headers and Keys

By default, the Grid extracts column headers from each column's [`title`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/columncomponent#title) or [`field`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/columncomponent#field) properties. Use the [`names`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#names) property to provide custom header names for the exported CSV file.

The [`keys`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#keys) property specifies which data fields to extract from each row object. By default, the Grid uses the `field` values from the column definitions.

```html
<kendo-grid>
    <kendo-grid-csv
        [names]="['ID', 'Customer', 'Amount']"
        [keys]="['id', 'customerName', 'totalAmount']">
    </kendo-grid-csv>
</kendo-grid>
```

### Formula Injection Prevention

The [`preventFormulaInjection`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#preventformulainjection) property protects against formula injection attacks in spreadsheet applications. When enabled (default is `true`), the Grid prefixes potentially dangerous formula characters (`=`, `+`, `-`, `@`, tab, `\r`) with a single quote (`'`) to prevent them from being interpreted as formulas.

```html
<kendo-grid>
    <kendo-grid-csv [preventFormulaInjection]="true"></kendo-grid-csv>
</kendo-grid>
```

### Cell Length Limits

The [`maxCellLength`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#maxcelllength) property sets the maximum number of characters allowed per cell. Values exceeding this limit are automatically truncated. The default value is `32767`, which matches Excel's cell character limit.

```html
<kendo-grid>
    <kendo-grid-csv [maxCellLength]="10000"></kendo-grid-csv>
</kendo-grid>
```

### Rows and Columns Limits

The [`maxRows`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#maxrows) and [`maxColumns`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/csvcomponent#maxcolumns) properties control the maximum number of rows and columns included in the export. These limits help prevent memory exhaustion when exporting large data sets and protect against potential security issues.

By default, the Grid exports up to 1,000,000 rows and 1,000 columns. When your data exceeds these limits, the export automatically truncates to the specified maximum values.

```html
<kendo-grid>
    <kendo-grid-csv [maxRows]="500000" [maxColumns]="50"></kendo-grid-csv>
</kendo-grid>
```

## Customizing Export Behavior

The [`csvExport`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#csvexport) event provides control over the CSV generation process. The event exposes the generated CSV string, which you can modify before the file is saved.

The following example demonstrates how to customize the exported CSV by adding a custom header with metadata and a footer with summary information.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/customize-export/source.txt)

Handle the `csvExport` event to:

* Add custom headers or footers to the CSV file.
* Modify cell values or formatting.
* Transform the CSV content before download.
* Prevent the default export and handle file saving manually.
* Log export operations or track usage.

## Triggering Export Externally

Besides using the built-in export button or toolbar tool, you can trigger the CSV export programmatically from your own custom UI elements or business logic. Call the [`saveAsCSV`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#saveascsv) method on the Grid component instance to initiate the export operation.

This approach is useful when you need to integrate the export functionality with custom buttons, menu items, or automated workflows. You can also combine it with other operations, such as validating data or applying filters before triggering the export.

The following example demonstrates how to trigger the CSV export using a custom button outside the Grid.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/csv/save-as-csv/source.txt)

## Known Limitations

* The Grid does not export cell templates or column templates. The export includes only the raw data values. To customize the exported content, transform your data before export or use the `fetchData` callback.
* The Grid does not export detail templates. For hierarchical data, you must flatten the structure in the `fetchData` callback or handle it in the `csvExport` event.
* The Grid does not apply column formats during export. CSV is a plain text format without formatting capabilities. If you need formatted values, transform the data in the `fetchData` callback before export.
* CSV files are plain text and do not support styling, colors, or advanced formatting. For formatted exports, use the [Excel Export](https://www.telerik.com/kendo-angular-ui-develop/components/grid/export/excel-export) feature instead.

## Suggested Links

* [Exporting Grid Data to Excel](https://www.telerik.com/kendo-angular-ui-develop/components/grid/export/excel-export)
* [Exporting Grid Data to PDF](https://www.telerik.com/kendo-angular-ui-develop/components/grid/export/pdf-export)
* [Printing the Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid/export/print-grid)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent)
* [API Index of the Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api)
