Skip to main content

CSV Export

Export grid data to a downloadable CSV file using the exportToCsv utility from core. It respects column visibility and applies valueFormatter so the exported values match what the user sees.

Live Demo

Click 'Export to CSV' to download the data
Live
Try it in your framework

The demo above uses Radix UI for styling. To see this feature with your framework's design system (Fluent UI, Material UI, Vuetify, PrimeNG, etc.), click "Open in online demo" below the demo.

Quick Example

import { exportToCsv } from '@alaarab/ogrid-react-radix';

function ExportButton({ data, columns }: { data: Row[]; columns: IColumnDef<Row>[] }) {
const handleExport = () => {
exportToCsv(
data,
columns.map((col) => ({ columnId: col.columnId, name: col.name })),
(item, columnId) => {
const col = columns.find((c) => c.columnId === columnId);
if (!col) return '';
const value = col.valueGetter ? col.valueGetter(item) : (item as Record<string, unknown>)[columnId];
return col.valueFormatter ? col.valueFormatter(value, item) : String(value ?? '');
},
'my-data.csv'
);
};

return <button onClick={handleExport}>Export to CSV</button>;
}
Switching UI libraries

The OGrid component has the same props across all React UI packages. To switch, just change the import:

  • Radix (lightweight, default): from '@alaarab/ogrid-react-radix'
  • Fluent UI (Microsoft 365 / SPFx): from '@alaarab/ogrid-react-fluent' - wrap in <FluentProvider>
  • Material UI (MUI v7): from '@alaarab/ogrid-react-material' - wrap in <ThemeProvider>

How It Works

The exportToCsv Function

exportToCsv<T>(
items: T[],
columns: CsvColumn[],
getValue: (item: T, columnId: string) => string,
filename?: string
): void
ParameterTypeDescription
itemsT[]The row data to export
columnsCsvColumn[]Array of { columnId, name } defining which columns to include and their header names
getValue(item, columnId) => stringFunction to extract the display value for each cell
filenamestring (optional)Download filename (default: 'export.csv')

The function:

  1. Builds a CSV header row from columns[].name.
  2. Iterates over each item and calls getValue for each column.
  3. Escapes values containing commas, quotes, or newlines.
  4. Triggers a browser download with the specified filename.

Context Menu Integration

When cellSelection is enabled (the default), the context menu does not include a dedicated "Export to CSV" item. Use the exportToCsv utility directly from a toolbar button or custom UI element.

Exporting Visible Columns Only

Filter the columns array to only include visible columns before passing to exportToCsv.

const visibleCols = columns.filter((col) => visibleColumns.has(col.columnId));

exportToCsv(
data,
visibleCols.map((col) => ({ columnId: col.columnId, name: col.name })),
getValue,
'filtered-export.csv'
);

Formatted Values

Use valueFormatter in your getValue function so exported data matches the display. For example, a currency column that shows "$1,234.56" in the grid will export that same string.

const getValue = (item: Row, columnId: string) => {
const col = columns.find((c) => c.columnId === columnId)!;
const raw = col.valueGetter ? col.valueGetter(item) : (item as any)[columnId];
return col.valueFormatter ? col.valueFormatter(raw, item) : String(raw ?? '');
};

Props Reference

TypeFieldDescription
CsvColumncolumnIdColumn identifier
CsvColumnnameHeader label in the CSV
  • Context Menu -- clipboard operations for quick copy/paste
  • Grid API -- access current data and column state programmatically
  • Column Chooser -- determines which columns are visible for export