Data Grid - Export
Easily export the rows in various file formats such as CSV, Excel, or PDF.
CSV export
The DataGrid allows the data to be exported to CSV by composing a toolbar with the GridToolbarExport
component. Use the components
prop to assign the custom toolbar.
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar,
}}
/>
Customize exported columns
By default, the CSV will only contain the visible columns of the grid. There are two ways to include or hide other columns:
- Define the exact columns to be exported with the
fields
attribute in thecsvOptions
prop ofGridToolbarExport
.
<GridToolbarExport csvOptions={{ fields: ['id', 'name'] }} />
Set allColumns
in csvOptions
to true to include hidden columns, instead of only the visible ones.
<GridToolbarExport csvOptions={{ allColumns: true }} />
- Set the
disableExport
attribute to true in eachGridColDef
.
<DataGrid columns={[{ field: 'id', disableExport: true }, { field: 'brand' }]} />
Export custom rendered 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
.
<GridToolbarExport 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) => void
getDataAsCsv()
Returns the grid data as a CSV string.
This method is used internally by exportDataAsCsv
.
Signature:
getDataAsCsv: (options?: GridCsvExportOptions) => string
Optimization of the layout of the grid for print mode. It can also be used to export to PDF.
The DataGrid provides the ability to optimize the layout of the grid for print mode. It can also be used to export to PDF. You can print the grid by composing a toolbar with the GridToolbarExport
component. Use the components
prop to assign the custom toolbar.
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar,
}}
/>
Customize printed columns
By default, when printing the grid it will only contain the visible columns. There are two ways to include or hide other columns:
- Define the exact columns to be exported with the
fields
attribute in theprintOptions
prop ofGridToolbarExport
.
<GridToolbarExport printOptions={{ fields: ['id', 'name'] }} />
Set allColumns
in printOptions
to true to include hidden columns, instead of only the visible ones.
<GridToolbarExport printOptions={{ allColumns: true }} />
- Set the
disableExport
attribute totrue
in eachGridColDef
.
<DataGrid columns={[{ field: 'id', disableExport: true }, { field: 'brand' }]} />
Export custom rendered cells
When the value of a field is an object or a renderCell
is provided, printing 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 print button
You can remove the Print export button from the toolbar by setting disableToolbarButton
option to true
.
<GridToolbarExport 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.