Birch UI v0.1
Pricing

CSV export

Export the grid's current view as CSV text or a downloadable file, with control over columns, separators, formatting and selection.

Open .md

exportCsv() returns the grid's data as a CSV string. downloadCsv() builds the same string and saves it as a file. Both export what the user is looking at: the current filters and sort order apply, columns appear in their current order, and hidden columns are left out. With pagination, every page of the result is exported, not only the current one.

export-orders.ts
import { createGrid } from '@birchui/core';

const grid = createGrid<Order>(document.querySelector('#orders')!, {
  columns,
  rows: orders,
  getRowId: (o) => o.id,
  rowSelection: 'multiple',
  checkboxSelection: true,
});

document.querySelector('#export')!.addEventListener('click', () => {
  grid.downloadCsv({ fileName: `orders-${new Date().toISOString().slice(0, 10)}.csv` });
});

Options#

CsvExportOptionsinterface@birchui/coreFull reference
PropertyTypeDescription
separatoroptionalstring—
includeHeadersoptionalboolean—
columnsoptionalstring[]—
onlySelectedoptionalboolean—
formattedoptionalboolean—
fileNameoptionalstring—
Option Default Meaning
separator ',' Field separator. Use ';' for spreadsheet apps in locales that use a decimal comma, or '\t' for TSV.
includeHeaders true Write a header row with the column headers.
columns All visible columns Restrict the export to these column IDs. Columns keep the grid's display order, and hidden columns are never exported.
onlySelected false Export only selected rows.
formatted false Write each column's formatted text (format or the type's default formatter) instead of the raw value.
fileName 'export.csv' File name for downloadCsv().

Raw or formatted values#

Raw values are best when the file feeds another system: numbers stay numbers and dates stay in their source representation (Date values are written as ISO 8601). Formatted values are best when a person will read the file: currency symbols, thousands separators and localized dates appear as they do in the grid.

TypeScript
const forImport = grid.exportCsv();

const forPeople = grid.exportCsv({ formatted: true, separator: ';' });

Choosing columns#

TypeScript
const csv = grid.exportCsv({
  columns: ['id', 'customer', 'region', 'revenue'],
});

To export a column that users normally keep hidden, show it for the export and hide it again:

TypeScript
grid.setColumnHidden('internalNotes', false);
const withNotes = grid.exportCsv();
grid.setColumnHidden('internalNotes', true);

The selection checkbox column is never exported.

Exporting the selection#

onlySelected: true exports the selected rows that pass the current filters, in display order.

TypeScript
document.querySelector('#export-selected')!.addEventListener('click', () => {
  if (grid.getSelectedIds().length === 0) return;
  grid.downloadCsv({ onlySelected: true, fileName: 'selected-orders.csv' });
});

What the file contains#

  • Lines end with \r\n, as RFC 4180 specifies.
  • Fields that contain the separator, a double quote or a line break are wrapped in double quotes, and embedded quotes are doubled.
  • Empty values (null and undefined) are written as empty fields.
  • The header row uses each column's header text.
  • Group and total rows from Grid Pro are exported with their aggregate values; full-width rows such as master/detail panels are skipped.
  • downloadCsv() prefixes the file with a UTF-8 byte order mark so spreadsheet apps detect the encoding and show accented customer names correctly.

Formula injection protection#

A cell whose text starts with =, +, -, @, a tab or a carriage return can be interpreted as a formula when the CSV is opened in a spreadsheet. The grid prefixes such string values with a single quote (=HYPERLINK("https://example.com") is written as '=HYPERLINK("https://example.com")), which spreadsheet apps display as text. Numbers are never prefixed, so negative amounts stay numeric.

Sending CSV to a server#

exportCsv() is synchronous and returns a string, so you can post it directly:

TypeScript
await fetch('/api/reports/orders', {
  method: 'POST',
  headers: { 'content-type': 'text/csv; charset=utf-8' },
  body: grid.exportCsv({ formatted: false }),
});

Excel files#

GridApiinterface@birchui/coreFull reference
MemberTypeDescription
exportCsv()(options?: CsvExportOptions) => string—
downloadCsv()(options?: CsvExportOptions) => void—

↑ ↓ to move, Enter to open, Esc to close