CSV Export Example
Material React Table does not have a data exporting feature built in. However, you can easily integrate your own exporting feature, if desired.
In the example below, export-to-csv
is connected to some export buttons in the top toolbar. If you need to export in Excel or PDF format, there are a variety of NPM packages that can be used to export in these formats.
ID | First Name | Last Name | Company | City | Country | |
---|---|---|---|---|---|---|
1 | Elenora | Wilkinson | Feest - Reilly | Hertaland | Qatar | |
2 | Berneice | Feil | Deckow, Leuschke and Jaskolski | Millcreek | Nepal | |
3 | Frieda | Baumbach | Heidenreich, Grady and Durgan | Volkmanside | Croatia | |
4 | Zachery | Brown | Cormier - Skiles | Faychester | Saint Pierre and Miquelon | |
5 | Kendra | Bins | Wehner - Wilderman | New Valentin | Senegal | |
6 | Lysanne | Fisher | Schmidt LLC | Malachitown | Costa Rica | |
7 | Garrick | Ryan | Ryan - Buckridge | East Pearl | Cocos (Keeling) Islands | |
8 | Hollis | Medhurst | Quitzon Group | West Sienna | Papua New Guinea | |
9 | Arlo | Buckridge | Konopelski - Spinka | Chino | Congo | |
10 | Rickie | Auer | Lehner - Walsh | Nyahfield | Sudan |
10
1import {2 MaterialReactTable,3 useMaterialReactTable,4 type MRT_Row,5 createMRTColumnHelper,6} from 'material-react-table';7import { Box, Button } from '@mui/material';8import FileDownloadIcon from '@mui/icons-material/FileDownload';9import { mkConfig, generateCsv, download } from 'export-to-csv'; //or use your library of choice here10import { data, type Person } from './makeData';1112const columnHelper = createMRTColumnHelper<Person>();1314const columns = [15 columnHelper.accessor('id', {16 header: 'ID',17 size: 40,18 }),19 columnHelper.accessor('firstName', {20 header: 'First Name',21 size: 120,22 }),23 columnHelper.accessor('lastName', {24 header: 'Last Name',25 size: 120,26 }),27 columnHelper.accessor('company', {28 header: 'Company',29 size: 300,30 }),31 columnHelper.accessor('city', {32 header: 'City',33 }),34 columnHelper.accessor('country', {35 header: 'Country',36 size: 220,37 }),38];3940const csvConfig = mkConfig({41 fieldSeparator: ',',42 decimalSeparator: '.',43 useKeysAsHeaders: true,44});4546const Example = () => {47 const handleExportRows = (rows: MRT_Row<Person>[]) => {48 const rowData = rows.map((row) => row.original);49 const csv = generateCsv(csvConfig)(rowData);50 download(csvConfig)(csv);51 };5253 const handleExportData = () => {54 const csv = generateCsv(csvConfig)(data);55 download(csvConfig)(csv);56 };5758 const table = useMaterialReactTable({59 columns,60 data,61 enableRowSelection: true,62 columnFilterDisplayMode: 'popover',63 paginationDisplayMode: 'pages',64 positionToolbarAlertBanner: 'bottom',65 renderTopToolbarCustomActions: ({ table }) => (66 <Box67 sx={{68 display: 'flex',69 gap: '16px',70 padding: '8px',71 flexWrap: 'wrap',72 }}73 >74 <Button75 //export all data that is currently in the table (ignore pagination, sorting, filtering, etc.)76 onClick={handleExportData}77 startIcon={<FileDownloadIcon />}78 >79 Export All Data80 </Button>81 <Button82 disabled={table.getPrePaginationRowModel().rows.length === 0}83 //export all rows, including from the next page, (still respects filtering and sorting)84 onClick={() =>85 handleExportRows(table.getPrePaginationRowModel().rows)86 }87 startIcon={<FileDownloadIcon />}88 >89 Export All Rows90 </Button>91 <Button92 disabled={table.getRowModel().rows.length === 0}93 //export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)94 onClick={() => handleExportRows(table.getRowModel().rows)}95 startIcon={<FileDownloadIcon />}96 >97 Export Page Rows98 </Button>99 <Button100 disabled={101 !table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()102 }103 //only export selected rows104 onClick={() => handleExportRows(table.getSelectedRowModel().rows)}105 startIcon={<FileDownloadIcon />}106 >107 Export Selected Rows108 </Button>109 </Box>110 ),111 });112113 return <MaterialReactTable table={table} />;114};115116export default Example;117
View Extra Storybook Examples