SpreadsheetGrid
Guides

CSV / Excel export

downloadCsv / exportCsv, and the library-independent getExportData

Export is called from the imperative handle (ref). CSV is built in; for Excel (xlsx) and other formats, pass the formatted data returned by getExportData() to any library to generate the file (no spreadsheet library is bundled).

Demo

Filter or sort first to see it reflected
Rows 6 of 6Columns 4 of 4Sort: —
#
1
2
3
4
5
6
Rows 6 of 6Columns 4 of 4
Cell —Range —0 cells / 0 rows0 columns

CSV

const gridRef = useRef<SpreadsheetGridHandle<Row>>(null);

// download as a file (with BOM, opens in Excel as is)
gridRef.current?.downloadCsv('products.csv');

// when you want the string
const csv = gridRef.current?.exportCsv({ delimiter: '\t' }); // TSV works too

Output scope (scope)

scopeTarget
'view' (default)All view rows after filtering and sorting (independent of the scroll position)
'raw'All original rows (ignores filtering and sorting)
'selection'Rows in the selected cell range
'rendered'Only rows currently rendered by virtualization (depends on scrolling)

'selection' outputs the selection at the time of the call (right after selectRange is fine; since 1.3.0). Exports right after changing sorting, filtering or columns with applyState and so on (in the same code) come out in the order before the table re-renders; to output in the new order, call after re-rendering (after the next render).

Columns are output as the visible columns, in display order. Values are raw values not passed through valueFormatter (String(value ?? ''); 1234.5 is 1234.5, not 1,234.5), the same rule as copy (Ctrl/Cmd+C). If there's a formatClipboardValue, that's used, so for columns you want output as on screen, write the same formatting as valueFormatter in formatClipboardValue.

Excluding rows (isRowExportable)

With the isRowExportable prop, you can narrow the rows targeted by "copy (TSV from Ctrl/Cmd+C) / exportCsv / getExportData". Rows for which it returns false are removed from output entirely (display, selection and paste are unaffected). It's meant for leaving filler rows on screen (placeholders and so on) out of output.

<SpreadsheetGrid
  rows={rows}
  columns={columns}
  rowKeyGetter={(row) => row.id}
  // leave placeholder rows out of copy / CSV / getExportData output
  isRowExportable={(row) => !row.isPlaceholder}
/>

ctx.viewRowIndex is the view row index after filtering and sorting (only with scope 'raw' is it the index in the original rows array), and ctx.rowKey is the rowKeyGetter value.

Excluding columns (exportable)

With exportable: false on a column, it's left out of CSV / TSV output (exportCsv / downloadCsv) and getExportData (since 1.3.0). Use it for action button columns, or internal ID columns shown on screen but not to be exported.

const columns: GridColumn<Order>[] = [
  { key: 'code', title: 'Order no.', width: 120 },
  { key: 'internalId', title: 'Internal ID', width: 100, exportable: false },
  { key: 'actions', title: '', width: 80, exportable: false, renderCell: ({ row }) => <button onClick={() => remove(row)}>Delete</button> },
];
  • It's left out with scope 'selection' (exporting the selected range) too.
  • Copy of a selected range (Ctrl/Cmd+C) still includes it as before (it copies what you selected). For columns you want left out of copy too, hide them from the table or don't select them.
  • Leaving out whole rows is isRowExportable above.

Including label rows (heading rows) (includeLabelRows)

Label rows (labelRow) aren't included in output by default (not output as data rows with scope 'raw' either). Pass includeLabelRows: true to output each heading as one row (without exportText, the getLabel text goes in the first column and the other columns are empty).

gridRef.current?.downloadCsv('products.csv', { includeLabelRows: true });
const data = gridRef.current?.getExportData({ includeLabelRows: true });
// tell heading rows apart with data.rowKinds[i] === 'label' → apply merged cells / bold in Excel

Including column group levels (includeColumnGroups)

Levels of column groups (columnGroups) aren't included in output by default. Pass includeColumnGroups: true to output one row per level before the header row (a group's name is written only in its first column and the rest are empty; not output with includeHeaders: false; since 1.6.0). In getExportData, columnGroups holds { key, title, start, span } per level, so you can merge cells in Excel.

gridRef.current?.downloadCsv('shipments.csv', { includeColumnGroups: true });
const data = gridRef.current?.getExportData({ includeColumnGroups: true });
// data.columnGroups[0] = [{ key: 'tokyo', title: 'Tokyo warehouse', start: 1, span: 4 }, …] (start is the position of the output column)

Excel (xlsx) and others

const data = gridRef.current?.getExportData(); // { columns, rows (2D cells) }
// build the workbook with any library: ExcelJS / SheetJS / hucre and so on

getExportData() only returns data, like a pure function, so multiple sheets can be built by calling it per scope or per grid. For detailed cell types, see the API reference.

On this page