Skip to content

Export Plugin

The Export plugin lets users download grid data as CSV, JSON, or other formats with a single click or API call. Great for reporting, data backup, or letting users work with data in Excel.

import '@toolbox-web/grid/features/export';

Enable the feature and call exportCsv() or exportJson() when you’re ready to download. You can also combine it with the selection feature to export only selected rows.

import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Name' },
{ field: 'email', header: 'Email' },
{ field: 'department', header: 'Department' }
],
features: {
export: {
fileName: 'employees',
includeHeaders: true,
},
},
};
// Trigger export via button click
document.getElementById('export-btn').addEventListener('click', () => {
const plugin = grid.getPluginByName('export');
plugin.exportCsv();
});
Include headers Include column headers in export
Visible only Export only visible columns
Selected only Export only selected rows

Export all visible data to CSV. Toggle “Selected only” and select some rows to export just those.

OptionTypeDefaultDescription
fileNamestring'export'Base filename (without extension)
includeHeadersbooleantrueInclude column headers in export
onlyVisiblebooleantrueExport only visible columns
onlySelectedbooleanfalseExport only selected rows (requires SelectionPlugin)

The ExportPlugin provides methods for exporting data with fine-grained control over which columns and rows to include. Like the ClipboardPlugin, it accepts columns and rowIndices parameters so you can export exactly the data you need.

const exportPlugin = grid.getPluginByName('export');
// Export all visible data to CSV
exportPlugin.exportCsv();
// Export to Excel XML
exportPlugin.exportExcel();
// Export to JSON
exportPlugin.exportJson();

Export with Column/Row Control (ExportParams)

Section titled “Export with Column/Row Control (ExportParams)”
const exportPlugin = grid.getPluginByName('export');
// Export specific columns
exportPlugin.exportCsv({
columns: ['name', 'email', 'department'],
fileName: 'contacts',
includeHeaders: true,
});
// Export specific rows
exportPlugin.exportCsv({
rowIndices: [0, 3, 7],
fileName: 'selected-employees',
});
// Export specific columns from specific rows
exportPlugin.exportExcel({
columns: ['name', 'salary'],
rowIndices: [0, 1, 2],
fileName: 'salary-report',
});
OptionTypeDefaultDescription
fileNamestringconfig valueFile name (without extension)
columnsstring[]-Specific column fields to export
rowIndicesnumber[]-Specific row indices to export
includeHeadersbooleanconfig valueInclude column headers in export
processCell(value, field, row) => any-Custom cell value processor
processHeader(header, field) => string-Custom header processor
mode'raw' | 'formatted''raw''raw' = underlying typed values; 'formatted' = what the grid displays (column.format + type defaults applied)
fileExtensionstring'.xls'Override file extension for Excel export (e.g. '.xml')
excelStylesExcelStyleConfig-Excel style configuration (Excel only)

The plugin exposes data accessors so you can drive the export pipeline without producing a download — useful for piping rows into a real OOXML writer (e.g. ExcelJS), copying CSV to the clipboard, sending data to a server, or pre-processing before download.

MethodReturnsPurpose
export(params?)Record<string, unknown>[]Resolve the rows that would be exported, keyed by column.field
formatCsv(data, params?, options?)stringFormat already-resolved rows as CSV (no download)
formatExcel(data, params?)stringFormat already-resolved rows as Excel XML (no download)
getResolvedColumns(params?)ColumnConfig[]The columns (in order) an export would include

All accessors honour onlyVisible, onlySelected, columns, and rowIndices (which columns/rows are included), and processCell (per-cell transformation — applied once on the values that get written).

export() additionally honours mode because it owns value resolution. formatCsv() / formatExcel() are pure formatters: they write whatever you pass in (after running processCell) and do not apply mode — use export({ mode: 'formatted' }) upstream if you need displayed values.

import { queryGrid } from '@toolbox-web/grid';
import type { ExportPlugin } from '@toolbox-web/grid/plugins/export';
const grid = queryGrid('tbw-grid');
const exporter = grid.getPluginByName('export') as ExportPlugin;
// 1. "Export what I see" — column.format applied
exporter.exportCsv({ mode: 'formatted' });
// 2. Get the rows without producing a file
const rows = exporter.export();
// 3. Compose: copy CSV to the clipboard
const csv = exporter.formatCsv(exporter.export());
await navigator.clipboard.writeText(csv);
// 4. Hand off raw rows to a real .xlsx writer
import ExcelJS from 'exceljs';
const cols = exporter.getResolvedColumns();
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet('Sheet1');
sheet.columns = cols.map((c) => ({ header: c.header ?? c.field, key: c.field }));
sheet.addRows(exporter.export());
const buffer = await workbook.xlsx.writeBuffer();
  • 'raw' (default) — values straight from the row (Date stays Date, numbers stay numbers). Identical to the pre-existing exportCsv / exportExcel / exportJson output, so opting into the new methods is non-breaking.
  • 'formatted' — applies the column-type default formatter and column.format(value, row), returning the same string the user sees in the cell. processCell runs last on the formatted value.

The export plugin produces XML Spreadsheet 2003 output — a single-XML format natively understood by Excel, without any external dependencies.

Pass an excelStyles object in ExportParams to control header styles, per-column formatting, dynamic cell styling, column widths, and more.

Click Export Styled Excel to download a formatted .xls file with bold headers, currency-formatted salaries, and color-coded status cells. Compare with Export Plain Excel to see the difference.

exportPlugin.exportExcel({
fileName: 'report',
excelStyles: {
headerStyle: {
font: { bold: true, size: 12, color: '#FFFFFF' },
fill: { color: '#4472C4' },
alignment: { horizontal: 'Center' },
},
defaultStyle: {
font: { name: 'Calibri', size: 10 },
},
},
});
exportPlugin.exportExcel({
fileName: 'financial-report',
excelStyles: {
headerStyle: { font: { bold: true }, fill: { color: '#D9E2F3' } },
columnStyles: {
revenue: { numberFormat: '$#,##0.00', alignment: { horizontal: 'Right' } },
margin: { numberFormat: '0.0%' },
date: { numberFormat: 'yyyy-mm-dd' },
},
columnWidths: { name: 25, revenue: 15, margin: 10, date: 12 },
},
});
exportPlugin.exportExcel({
fileName: 'status-report',
excelStyles: {
cellStyle: (value, field) => {
if (field === 'status') {
if (value === 'Active') return { fill: { color: '#C6EFCE' }, font: { color: '#006100' } };
if (value === 'Inactive') return { fill: { color: '#FFC7CE' }, font: { color: '#9C0006' } };
}
return undefined; // fall through to column/default style
},
},
});
exportPlugin.exportExcel({
excelStyles: { autoFitColumns: true },
});
OptionTypeDefaultDescription
headerStyleExcelCellStyle-Style applied to all header cells
defaultStyleExcelCellStyle-Default style for all data cells
columnStylesRecord<string, ExcelCellStyle>-Per-column style overrides (keyed by field)
cellStyle(value, field, row) => ExcelCellStyle | undefined-Callback for per-cell dynamic styling
columnWidthsRecord<string, number>-Column widths in characters (keyed by field)
autoFitColumnsbooleanfalseAuto-fit column widths from content
PropertyTypeDescription
fontobject{ name?, size?, bold?, italic?, color? }
fillobject{ color, pattern? } — pattern defaults to 'Solid'
numberFormatstringExcel format code (e.g. '#,##0.00', '0%', 'yyyy-mm-dd')
alignmentobject{ horizontal?, vertical?, wrapText? }
bordersobject{ top?, bottom?, left?, right? } — each { style, color? }
Event Log:
EventDetailDescription
export-complete{ format, fileName, rowCount, columnCount }Fired after an export completes

The ExportPlugin exports the rows currently held in memory (grid.rows). It has no async or server-side support — it cannot fetch data it hasn’t already loaded.

AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt