# Import CSV Data into the Grid

## Environment

| Product         | Progress® KendoReact Grid, Progress® KendoReact Upload |
| --------------- | ------------------------------------------------------ |
| Product Version | 16.2.0                                                 |

## Description

How can I import data from a CSV file into the KendoReact Grid? Does the Grid provide a built-in CSV import?

This KB also answers the following questions:

* Can I upload a CSV file and show its records in the Grid?
* How do I read the content of a file that is selected in the KendoReact Upload?
* Does the `@progress/kendo-csv` package parse CSV files?

## Solution

The Grid supports [exporting data to CSV](https://www.telerik.com/kendo-react-ui/components/grid/export/csv-export), but it does not provide a built-in CSV import. The `@progress/kendo-csv` package that the Grid uses for the export also only generates CSV content.

To import a CSV file, compose the existing components. Use the [Upload](https://www.telerik.com/kendo-react-ui/components/upload) component to select the file, read the file content with the browser `FileReader`, parse the text into records, and bind the records to the [`data`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#data) property of the Grid.

The following example demonstrates the complete implementation. Download the sample CSV file, and then select it in the Upload to load its records into the Grid.

[Demo Source](https://demos.telerik.com/kendo-react-ui/knowledge-base/grid/grid-import-csv-data/source.txt)

The solution consists of the following steps:

1. Set the [`saveUrl`](https://www.telerik.com/kendo-react-ui/components/upload/api/uploadprops#saveurl) property of the Upload to a function. The function receives the selected [`UploadFileInfo`](https://www.telerik.com/kendo-react-ui/components/upload/api/uploadfileinfo), and you can read the raw file through its `getRawFile` method. Limit the selection to CSV files by using the [`restrictions`](https://www.telerik.com/kendo-react-ui/components/upload/restrictions) property. The following snippet shows the restriction.

   ```tsx
   <Upload restrictions={{ allowedExtensions: ['.csv'] }} saveUrl={onSaveRequest} />
   ```

2. In the `saveUrl` function, read the raw file as text with a `FileReader`, parse the text, and save the result in the state of the component. The following snippet shows how the demo reads the file.

   ```tsx
   const reader = new FileReader();

   reader.onload = () => {
       if (typeof reader.result === 'string') {
           setData(toRecords(reader.result));
           resolve({ uid });
       }
   };

   reader.readAsText(currentFile.getRawFile!());
   ```

3. Bind the records to the Grid, and generate the columns from the keys of the first record.

   ```tsx
   <Grid data={data}>
       {Object.keys(data[0]).map((name) => (
           <GridColumn key={name} field={name} title={name} />
       ))}
   </Grid>
   ```

> The `parseCsv` function in the demo handles quoted fields, escaped quotes, and line breaks inside quoted fields, and it assumes a comma delimiter and a header row. For other CSV dialects or advanced scenarios, use a dedicated CSV parsing library of your choice.

## See Also

* [CSV Export in the Grid](https://www.telerik.com/kendo-react-ui/components/grid/export/csv-export)
* [Upload File Processing](https://www.telerik.com/kendo-react-ui/components/upload/file-processing)
* [Upload File Restrictions](https://www.telerik.com/kendo-react-ui/components/upload/restrictions)
* [Uploaded File to Base64](https://www.telerik.com/kendo-react-ui/components/upload/base64)
