Exporting (CSV & XLSX) in Apex Grid

The Apex grid exports rows to CSV or XLSX without pulling in any runtime dependencies. The CSV writer follows RFC 4180 and emits a UTF-8 BOM so Excel opens the file with the correct encoding. The XLSX writer preserves native cell types for numbers, booleans, and Date values.

Both exports are available as public methods on the grid and, optionally, as a dropdown in the toolbar.

Programmatic API

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

grid.exportToXLSX({ filename: 'users', sheetName: 'Users' });

Passing filename: '' to exportToCSV 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.

Options

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

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

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.
  • 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.

{
  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 with Export CSV and Export XLSX items that call the same public methods.

<apex-grid show-export></apex-grid>

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.