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-gridpackage. 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. CallingexportToXLSX()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 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 ingrid.selectedRows.'all'— the entire underlyingdataarray, regardless of view state.
- filename is the suggested download name (without extension). Pass
''toexportToCSVto 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.
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.