Exporting (CSV & XLSX) in Apex Grid

The Apex grid exports rows without pulling in any runtime dependencies.

  • CSV export is free: it ships in the community apex-grid package. The writer follows RFC 4180 and emits a UTF-8 BOM so Excel opens the file with the correct encoding.
  • Excel (XLSX) export is an enterprise feature: as of v3 it lives in apex-grid-enterprise, not core. Calling exportToXLSX() requires the <apex-grid-enterprise> element.

Upgrading from v2, where exportToXLSX() lived on <apex-grid>? See the v2 → v3 migration guide.

CSV export (community)

exportToCSV() is a public method on <apex-grid>:

grid.exportToCSV();                                            // downloads data.csv
grid.exportToCSV({ filename: 'users', source: 'selected' });
const text = grid.exportToCSV({ filename: '' });               // returns the CSV text

Passing filename: '' returns the generated text instead of triggering a download, useful when handing the payload off to a server or stitching it into a larger archive.

XLSX export (enterprise)

exportToXLSX() is available on <apex-grid-enterprise>. The XLSX writer preserves native cell types for numbers, booleans, and Date values.

import 'apex-grid-enterprise/define';

const grid = document.querySelector('apex-grid-enterprise');
grid.exportToXLSX({ filename: 'users', sheetName: 'Users' });

See the Enterprise docs for installation, licensing, and the full XLSX API.

Options

type ExportSource = 'view' | 'page' | 'selected' | 'all';

type ExportOptions = {
  filename?: string;
  source?: ExportSource;
  columns?: string[];
};

type CSVExportOptions = ExportOptions & {
  delimiter?: string;       // default ','
  bom?: boolean;            // default true: a UTF-8 byte-order mark, so Excel detects the encoding
  newline?: string;         // default '\r\n' (RFC 4180)
  escapeFormulas?: boolean; // default true, see below
};

type XLSXExportOptions = ExportOptions & {
  sheetName?: string;
};
  • source controls which rows are written:
    • 'view' (default): every row currently visible after sort, filter, and quick-filter.
    • 'page': only the current pagination slice.
    • 'selected': only rows in grid.selectedRows.
    • 'all': the entire underlying data array, regardless of view state.
  • filename is the suggested download name (without extension). Pass '' to exportToCSV to receive the string instead of triggering a download.
  • columns restricts the output to the listed column keys, in display order. Omit it to export every exportable column.
  • sheetName is the XLSX worksheet name; defaults to Sheet1.

Formulas in CSV text

From 3.6 a CSV export neutralises spreadsheet formulas. A text cell or header that starts with =, +, -, @, a tab or a carriage return is written with a leading apostrophe, so a spreadsheet opening the file shows =HYPERLINK(...) as text instead of running it. Numbers are written as they are, and so is text that is a plain signed decimal such as -5 or +1.5.

Other text with a leading sign gains the apostrophe too: a lone -, a phone number such as +44 20 7946 0958, -1,234.50, or formatter output such as -$5.00. Pass escapeFormulas: false for the previous output when the file goes to a program, not a spreadsheet:

grid.exportToCSV({ filename: 'raw', escapeFormulas: false });

On <apex-grid-enterprise>, exportToCSV({ formulas: true }) writes the sources of formula cells unescaped, with references rewritten to the cells of the file (below the header, in the exported order), while other text keeps the escaping. exportToXLSX({ formulas: true }) writes real formula cells with their computed values. Copying a range follows the same escaping rule; see range selection.

How values are written

  • Booleans export as the checkbox shows them: true only for true, so strings such as 'false' or 'yes' and the number 1 export as false (from 3.6).
  • Number, rating, currency and progress columns export as numbers; text that is not a number exports as an empty cell (currency and progress from 3.6).
  • Dates stored as 'YYYY-MM-DD' strings carry the calendar day the grid shows, in every time zone. An invalid Date exports as an empty cell.
  • With source: 'selected', rows are written in the order the grid shows them, followed by selected rows the view hides.

Per-column opt-out

Mark a column with exportable: false to omit it from the output. The column still renders in the grid; it is only skipped during export. This applies to both CSV and XLSX.

{
  key: 'actions',
  exportable: false,
}

Toolbar dropdown

Set show-export to render a download button on the trailing side of the toolbar. The button opens an accessible menu whose entries come from the grid's export formats:

  • On the community <apex-grid> the menu offers Export CSV.
  • On <apex-grid-enterprise> the menu additionally offers Export XLSX.
<apex-grid show-export></apex-grid>
<apex-grid-enterprise show-export></apex-grid-enterprise>

The trigger advertises aria-haspopup="menu" and aria-expanded; the menu uses role="menu" with role="menuitem" items and supports Arrow / Home / End navigation. The menu closes on Escape, an outside pointer click, or Tab.

Adding your own export formats

The toolbar menu is driven by a generic seam, so a derived grid can register additional formats:

  • get exportFormats(): ReadonlyArray<ExportFormat>: defaults to [{ id: 'csv', … }]. The enterprise grid extends it with the XLSX entry.
  • exportAs(formatId, options): the toolbar dispatches the chosen format here.