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
Name
Email
Department
Salary
Alice Johnson
alice@example.com
Engineering
$50,000
Bob Smith
bob@example.com
Marketing
$53,500
Carol Lee
carol@example.com
Sales
$57,000
David Kim
david@example.com
Finance
$60,500
Eve Torres
eve@example.com
Operations
$64,000
Frank Wu
frank@example.com
Engineering
$67,500
Grace Park
grace@example.com
Marketing
$71,000
Henry Adams
henry@example.com
Sales
$74,500
Irene Costa
irene@example.com
Finance
$78,000
Jack Rivera
jack@example.com
Operations
$81,500
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