Skip to content

Data Export

NextGrid exports client and loaded remote rows asynchronously in frame-friendly chunks to CSV, JSON, Markdown, and zero-dependency lazy Excel (.xlsx).

CSV, JSON, Markdown & Zero-Dep Excel Export
import { Grid } from 'next-grid-js/core';
import { ExportModule } from 'next-grid-js/export';
import { createOrders, type OrderRow } from './data/orders';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
import { addButton } from './ui';
export function mount({
stage,
controls,
meta,
}: DemoMountContext): () => void {
const grid = new Grid<OrderRow>({
container: stage,
modules: [ExportModule],
columnDefs: [
{
key: 'id',
name: 'ID',
width: 96,
isSortable: true,
},
{
key: 'customer',
name: 'Customer',
width: 175,
flex: 1,
isSortable: true,
},
{
key: 'region',
name: 'Region',
width: 105,
isSortable: true,
},
{
key: 'amount',
name: 'Amount',
width: 115,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
{
key: 'orderedAt',
name: 'Order Date',
width: 115,
cellDataType: 'date',
},
],
rowData: createOrders(35),
getRowId: (row) => row.id,
rowSelection: {
mode: 'multiple',
showCheckbox: true,
},
dataExport: {
formats: ['csv', 'xlsx', 'json', 'markdown'],
fileName: ({ format }) => `orders-demo.${format}`,
},
});
const stopTheme = syncGridTheme(stage);
addButton(controls, 'Preview CSV', async () => {
const res = await grid.exportData({
format: 'csv',
rows: 'filtered',
});
meta.textContent = `CSV: ${res.rowCount} rows (${res.text.length} chars)`;
});
addButton(controls, 'Preview JSON', async () => {
const res = await grid.exportData({
format: 'json',
rows: 'filtered',
});
meta.textContent = `JSON: ${res.rowCount} rows (${res.text.length} chars)`;
});
addButton(controls, 'Download XLSX', async () => {
const res = await grid.downloadExport({
format: 'xlsx',
rows: 'filtered',
});
meta.textContent = `Downloaded ${res.fileName} (${res.bytes.byteLength} bytes)`;
});
meta.textContent = 'Use footer Export or toolbar buttons';
return () => {
grid.destroy();
stopTheme();
};
}
Format Result Type Default values Notes
'csv' DataExportTextResult (text) 'formatted' RFC 4180 quoting, configurable separator, optional bom (default true), neutralizeFormulas
'json' DataExportTextResult (text) 'raw' Array of objects keyed by colId
'markdown' DataExportTextResult (text) 'formatted' Pipe-delimited table with escaped | and newlines
'xlsx' DataExportExcelResult (bytes: Uint8Array) 'formatted' Lazy chunk loaded only on .xlsx export; writes typed numbers, booleans, and dates from raw values
  • rows (DataExportRows): 'filtered' (default for client sources, across all pages), 'page', 'selected', or 'loaded' (only scope for remote infinite, serverSide, and viewport sources).
  • Cancellation: Pass signal: AbortSignal to exportData or downloadExport; aborting rejects with an AbortError.
  • File Naming (fileName): Accepts a string or (context: DataExportFileNameContext) => string; defaults to export-YYYY-MM-DD-HHmm.
  • Hierarchies & Exclusions: Grouped data exports leaf rows with decoded group paths; tree data exports parent rows; hidden columns, selection checkbox columns, pinnedRows, pinnedTotals, and pivot mode are not exported.