Data Grid - Export
Easily export the rows in various file formats such as CSV, Excel, or PDF.
CSV export
The easiest way to enable the CSV export is to pass the GridToolbar component in the Toolbar component slot.
<DataGrid {...data} loading={loading} components={{ Toolbar: GridToolbar }} /><DataGrid
{...data}
loading={loading}
components={{
Toolbar: CustomToolbar,
}}
/>Custom exported content
The csv export can be customized by passing a csvOptions object either to the GridToolbar or to the GridToolbarExport as a prop.
The following sections describes some customizations available on this interface.
<DataGrid componentsProps={{ toolbar: { csvOptions } }} />
// same as
<GridToolbarExport csvOptions={csvOptions} />
Exported columns
By default, the CSV will only contain the visible columns of the grid. There are a few ways to include or hide other columns:
- Set the exact columns to be exported in
csvOptions.
<DataGrid
componentsProps={{ toolbar: { csvOptions: { fields: ['id', 'name'] } } }}
/>
- Set
allColumnsincsvOptionstotrueto include hidden columns, instead of only the visible ones.
<DataGrid componentsProps={{ toolbar: { csvOptions: { allColumns: true } } }} />
- Set the
disableExportattribute totruein eachGridColDef.
<DataGrid columns={[{ field: 'id', disableExport: true }, { field: 'brand' }]} />
Exported rows
By default, if some rows are selected, the DataGrid exports only those.
If there's no selection, it exports all rows (filtered and sorted rows, if any rules are active), including the collapsed ones.
Alternatively, you can set the getRowsToExport function and export any rows you want, as in the following example.
The grid exports a few selectors that can help you get the rows for the most common use-cases:
| Selector | Behavior |
|---|---|
gridRowIdsSelector |
The rows in their original order. |
gridSortedRowIdsSelector |
The rows after applying the sorting rules. |
gridFilteredSortedRowIdsSelector |
The rows after applying the sorting rules and the filtering rules. |
gridVisibleSortedRowIdsSelector |
The rows after applying the sorting rules, the filtering rules and without the collapsed rows. |
gridPaginatedVisibleSortedGridRowIdsSelector |
The rows after applying the sorting rules, the filtering rules, without the collapsed rows and only for the current page (Note: If the pagination is disabled, it will still take the value of page and pageSize). |
When using Row grouping, it can be useful to remove the groups from the CSV export
Exported cells
When the value of a field is an object or a renderCell is provided, the CSV export might not display the value correctly.
You can provide a valueFormatter with a string representation to be used.
<DataGrid
columns={[
{
field: 'progress',
valueFormatter: ({ value }) => `${value * 100}%`,
renderCell: ({ value }) => <ProgressBar value={value} />,
},
]}
/>
Remove the export button
You can remove the CSV export option from the toolbar by setting disableToolbarButton option to true.
<DataGrid
componentsProps={{ toolbar: { csvOptions: { disableToolbarButton: true } } }}
/>
apiRef
โ ๏ธ Only use this API as the last option. Give preference to the props to control the grid.
exportDataAsCsv()
Downloads and exports a CSV of the grid's data.
Signature:
exportDataAsCsv: (options?: GridCsvExportOptions) => voidgetDataAsCsv()
Returns the grid data as a CSV string.
This method is used internally by exportDataAsCsv.
Signature:
getDataAsCsv: (options?: GridCsvExportOptions) => stringPrint export
The DataGrid provides the ability to optimize the layout of the grid for print mode. It can also be used to export to PDF.
The easiest way to enable the Print export is to pass the GridToolbar component in the Toolbar component slot.
<DataGrid {...data} loading={loading} components={{ Toolbar: GridToolbar }} /><DataGrid
{...data}
loading={loading}
components={{
Toolbar: CustomToolbar,
}}
/>Custom exported content
The print export can be customized by passing a printOptions object either to the GridToolbar or to the GridToolbarExport as a prop.
The following sections describes some customizations available on this interface.
<DataGrid componentsProps={{ toolbar: { printOptions }}} />
// same as
<GridToolbarExport printOptions={printOptions} />
Exported columns
By default, the Print will only contain the visible columns of the grid. There are a few ways to include or hide other columns:
- Set the exact columns to be exported in
printOptions.
<DataGrid
componentsProps={{ toolbar: { printOptions: { fields: ['id', 'name'] } } }}
/>
- Set
allColumnsinprintOptionstotrueto include hidden columns, instead of only the visible ones.
<DataGrid componentsProps={{ toolbar: { printOptions: { allColumns: true } } }} />
- Set the
disableExportattribute to true in eachGridColDef.
<DataGrid columns={[{ field: 'id', disableExport: true }, { field: 'brand' }]} />
Exported cells
When the value of a field is an object or a renderCell is provided, the Print export might not display the value correctly.
You can provide a valueFormatter with a string representation to be used.
<DataGrid
columns={[
{
field: 'progress',
valueFormatter: ({ value }) => `${value * 100}%`,
renderCell: ({ value }) => <ProgressBar value={value} />,
},
]}
/>
Remove the export button
You can remove the Print export option from the toolbar by setting disableToolbarButton option to true.
<DataGrid
componentsProps={{ toolbar: { printOptions: { disableToolbarButton: true } } }}
/>
apiRef
โ ๏ธ Only use this API as the last option. Give preference to the props to control the grid.
exportDataAsPrint()
Print the grid's data.
Signature:
exportDataAsPrint: (options?: GridPrintExportOptions) => void๐ง Excel export
โ ๏ธ This feature isn't implemented yet. It's coming.
๐ Upvote issue #198 if you want to see it land faster.
You will be able to export the displayed data to Excel with an API call, or using the grid UI.
๐ง Clipboard
โ ๏ธ This feature isn't implemented yet. It's coming.
๐ Upvote issue #199 if you want to see it land faster.
You will be able to copy and paste items to and from the grid using the system clipboard.