View as Markdown

Export and print

Export

Excel, PDF and CSV export of the grid view with hooks, events, several grids per workbook, server export and on-demand writers.

The grid exports what it shows: the current sort, filter, search and grouping, over every page (pages: 'all', the default) or the current page (pages: 'current'). Three instance methods cover the formats:

const file = await api.exportToExcel({ fileName: 'orders' }); //
await api.exportToPdf({ orientation: 'landscape' });          //
await api.exportToCsv({ delimiter: ';', bom: true });         //

Each resolves to { fileName, mimeType, data } and downloads it; pass download: false to keep only the file, and downloadBlob(file) saves any file or Blob later (it is a no-op on the server).

Writers load on demand

CSV escapes spreadsheet formulas by default: text starting with =, +, -, @, tab or CR (also after leading whitespace) gets a leading '; real numbers stay as they are. escapeFormulas: false restores raw output.

CSV is a pure writer in @salmonui/data-grid/engine and needs nothing. Excel and PDF writers live in @salmonui/export and are passed as loaders, so the default Data Grid bundle never contains them:

const writers = {
  excel: () => import('@salmonui/export/xlsx'),
  pdf: () => import('@salmonui/export/pdf'),
};
<DataGrid exportWriters={writers} toolbar={['exportExcel', 'exportPdf', 'exportCsv', 'print']} contextMenu />

The toolbar and context-menu export items run the export with default options. Without a loader the matching item is disabled and its title says why. To customize what an item does, register a handler: api.registerCommandHandler('export.excel', () => void api.exportToExcel({ ... })). A status with a spinner shows while a file is produced; it is a polite role="status" element, so screen readers announce "Exporting…" (localeText.exporting).

Options

  • rows exports custom data with the grid's columns, for example api.getSelectedRows(); includeHiddenColumns adds hidden columns; columns picks and relabels columns; fileName names the file.
  • Excel: sheetName, styles (header, row, groupCaption, aggregate), header / footer rows, freezeHeader, and mergeRepeatedValues, which merges equal adjacent values of a column into one cell.
  • PDF: orientation, pageSize, header / footer with {page} and {pages}, columnOverflow: 'fit' | 'paginate', font (see below), and mergeRepeatedValues as in Excel. In PDF a merged run restarts at every page (the value is drawn again at the top of the next page) and stops at group captions and detail blocks.
  • Formatting follows the column formats. Lookup columns export their display text, not the key. Stacked headers export as merged header rows, split at pinned-column edges like the grid header.
// Only the rows the user selected, with the grid's columns.
api.exportToExcel({ rows: api.getSelectedRows(), fileName: 'selected-orders' });
// Hidden columns too.
api.exportToCsv({ includeHiddenColumns: true });

Several grids in one workbook

grids adds further grids to the same Excel file. layout: 'sheets' (the default) writes one sheet per grid, named by sheetNames; layout: 'stacked' writes them one below the other on a single sheet with gap blank rows between them:

api.exportToExcel({ grids: [targetsApi], layout: 'sheets', sheetNames: ['Orders', 'Targets'] });
api.exportToExcel({ grids: [targetsApi], layout: 'stacked', gap: 2 });

PDF takes one grid per file. Multiple-grid export runs in the browser only; the server entry exports one grid.

Multiple-grid and outline limits

Two limits are intentional. Multiple-grid export composes the live export models of several grids in one client call; a server job would need every grid's query and data source, so there is no server-side multiple-grid export. Excel outlines stop at 7 levels, the SpreadsheetML maximum: deeper group and detail rows are still written at level 7, and child grids nested deeper than seven levels are left out. To export several grids into one file, call exportToExcel({ grids }) (or exportXlsx from @salmonui/export/xlsx with every grid) on the client.

Hooks and events

Hooks are functions on the export options and return patches:

  • cell(ctx) returns { value, text, style, formula, colSpan, rowSpan, image } per data cell. formula writes a live Excel formula, with or without the leading =. Security: never build formula from untrusted input (user data, imported files, API responses); it is written as a live formula that runs when the workbook is opened. Text cells never execute: formula-like text stays a string and gets Excel's quote-prefix style, with no visible '. colSpan / rowSpan merge the cell with the following cells and rows in Excel and PDF; the covered cells are not written. In PDF a column span is clipped to its column page set (columnOverflow: 'paginate'), and a row span stops at a group caption or detail block and continues on the next page with the cell drawn again.
  • The grid's own spans (column colSpan / rowSpan, mergeCells) are view settings and do not reach the export: return the same spans from cell(ctx), or pass mergeRepeatedValues, to merge exported cells.
  • A column with renderCell exports its value text; React output never becomes export text by itself. Return { text } from cell for the exported text of a templated column (a text patch without value writes a text cell, even on a number, boolean or date column), and image with PNG or JPEG bytes ({ data, type, width, height }) to draw a picture in the Excel or PDF cell; CSV keeps the text.
  • headerCell(ctx), aggregateCell(ctx) and groupCaption(ctx) customize header, aggregate and caption cells; group captions and footer, group footer and caption aggregates are exported.
  • detail(ctx) writes a detail block after a row: text lines ({ text: [...] }) or a nested export model ({ model }). hierarchy picks the rows: 'expanded' (default, rows whose detail is expanded; collapsed rows get no block, also from the hook), 'all' or 'none'. A grid without detail rows (no renderDetail or childGrid) has no expansion state, so its detail hook runs for every row unless the mode is 'none'. Excel nests detail blocks one outline level deeper and clamps at 7 outline levels.
api.exportToExcel({
  hierarchy: 'all',
  detail: ({ row }) => (row.note ? { text: [`Note: ${row.note}`] } : null),
  cell: ({ column, row }) => {
    if (column.field !== 'shipped') return undefined;
    const label = row.shipped ? 'Shipped' : 'Pending';
    return { value: label, text: label };
  },
});

onBeforeExport runs before every export and cancels it with preventDefault(); onExport receives the finished file. Both carry format: 'excel' | 'pdf' | 'csv' and the options.

Unicode text in PDF

The built-in PDF fonts are the standard Helvetica faces, which cover only Western European text; other characters print as ?. Pass TrueType bytes (.ttf, or the first face of a .ttc) as font and every text of the PDF is drawn with that font, embedded as a subset with a ToUnicode map, so the text stays searchable and copyable. CJK, Cyrillic, Greek and Arabic (with shaping and basic bidi) work when the font covers them. font: { regular, bold } adds a real bold face; without it bold is synthetic. No font ships with the package, and CFF-based OpenType (.otf) fonts are not supported.

const font = new Uint8Array(await (await fetch('/fonts/NotoSansJP-Regular.ttf')).arrayBuffer());
api.exportToPdf({ font, header: '注文' });

Server export

@salmonui/data-grid/engine/export-server builds the same export on a server. The client sends its view as serializeDataGridState(api.getState()); the server rebuilds a headless grid from the same rows and column definitions with createExportDataGrid, or passes { rows, columns, state } straight to exportToResponse (a Web Response for route handlers) or exportToStream (a Node Writable or Web WritableStream):

// app/api/export/route.ts (Next.js route handler)
import { exportToResponse } from '@salmonui/data-grid/engine/export-server';

export async function POST(request: Request) {
  const { format, state } = await request.json();
  return exportToResponse({ rows, columns, state, timeZone: 'UTC' }, { format, fileName: 'orders' });
}

The sort, filters, search, grouping and column order of the client apply; selection and scroll do not. Keep the column definitions in a module both sides import. Lookup columns export their display text on the server too; remote lookup sources are loaded before the file is written. Multiple-grid workbooks stay client-side.

The demo below runs every option above; its Server export buttons call a route like the one shown.

Export and print

Open demo

Excel, PDF and CSV export of 10,000 grouped rows with stacked headers, aggregates, lookup text, templates, hidden columns, selected rows, detail blocks, a second grid in the same workbook, an embedded PDF font and a server export route; writers load on demand; print of every row of the scope.

Order
Customer
Product
Qty
Amount
Discount
Category
Status
Name
City
Country: France - 100 items
ORD-10000
Jun Park
Reims
Green tea
1
$8.33
10%
Loose-leaf tea
Shipped
ORD-10004
Ines Duarte
Lyon
Espresso beans
7
$110.08
15%
Coffee beans
Shipped
ORD-10006
Goran Petrov
Lyon
Grinder
2
$258.00
0%
Brewing equipment
Pending
ORD-10010
Bao Nguyen
Lyon
Pour-over kit
12
$504.00
0%
Brewing equipment
Pending
ORD-10012
Goran Petrov
Lyon
Pour-over kit
11
$392.70
15%
Brewing equipment
Shipped
ORD-10020
Ines Duarte
Reims
Pour-over kit
10
$399.00
5%
Brewing equipment
Pending
ORD-10022
Hana Sato
Lyon
Oolong tea
11
$140.25
0%
Loose-leaf tea
Shipped
ORD-10033
Jun Park
Lyon
Kettle
5
$324.50
0%
Brewing equipment
Shipped
ORD-10036
Goran Petrov
Reims
Kettle
6
$369.93
5%
Brewing equipment
Shipped
ORD-10039
Jun Park
Lyon
Filter beans
12
$151.20
10%
Coffee beans
Pending
ORD-10040
Farah Aziz
Lyon
Grinder
8
$877.20
15%
Brewing equipment
Pending
ORD-10048
Ines Duarte
Reims
Green tea
6
$47.18
15%
Loose-leaf tea
Pending
ORD-10049
Elif Kaya
Lyon
Kettle
5
$324.50
0%
Brewing equipment
Shipped
ORD-10061
Hana Sato
Lyon
Kettle
12
$778.80
0%
Brewing equipment
Pending
ORD-10063
Goran Petrov
Lyon
Green tea
7
$61.51
5%
Loose-leaf tea
Shipped
ORD-10066
Bao Nguyen
Lyon
Green tea
7
$55.04
15%
Loose-leaf tea
Shipped
ORD-10071
Aiko Tan
Reims
Oolong tea
8
$96.90
5%
Loose-leaf tea
Pending
ORD-10076
Bao Nguyen
Reims
Green tea
1
$7.86
15%
Loose-leaf tea
Shipped
ORD-10082
Hana Sato
Lyon
Oolong tea
1
$10.84
15%
Loose-leaf tea
Pending
Region
Country
Target
Stretch (+10%)
Europe
France
$180,000
$198,000
Europe
Germany
$210,000
$231,000
Europe
Portugal
$95,000
$104,500
Asia
Japan
$240,000
$264,000
Asia
Malaysia
$120,000
$132,000
No events yet.