Export options
Look up the options of exportGrid and printGrid, the props of the export and print buttons, and the helper functions of data-grid-io.
The data-grid-io add-on exports the rows of the grid to a CSV or XLSX file and prints them as a paged table. exportGrid and printGrid take the read state of the grid and an optional options object. The read state is useDataGridStoreApi().getState(), from @/components/data-grid/data-grid-sdk. useDataGridApi().store.getState() returns the same state (see External store). The hooks useDataGridExport and useDataGridPrint bind that state for you. Export explains the behavior.
This sample adds the export button and the print button:
import {
DataGridExportButton,
DataGridPrintButton,
} from "@/components/data-grid-io/data-grid-io";
export function IoButtons() {
return (
<>
<DataGridExportButton />
<DataGridPrintButton />
</>
);
}Render the buttons inside DataGridProvider, for example in DataGridToolbar.
Functions
| Function | Returns | What it does |
|---|---|---|
exportGrid(state, options?) | Promise<ExportResult> | Writes a CSV or XLSX file and starts the download. |
printGrid(state, options?) | Promise<PrintGridResult> | Opens the print dialog for a paged table. It does nothing without a DOM. |
buildExportRows(state, scope, options?) | string[][] | Builds the text rows of the visible columns, without a header row. options.text is "export" (default) or "display", and options.maxRows caps the rows (BuildExportRowsOptions). |
rowsToCsv(rows, options?) | string | Joins rows into CSV with CRLF line ends. options.delimiter defaults to ",". options.sanitizeFormulas defaults to true. |
Example
This sample downloads orders.csv with semicolons, no byte order mark, the selected rows, and at most 1,000 data rows:
import {
useDataGridExport,
} from "@/components/data-grid-io/data-grid-io";
export function ExportSelectionButton() {
const { exportGrid } = useDataGridExport();
return (
<button
onClick={() =>
exportGrid({
format: "csv",
scope: "selection",
csvDelimiter: ";",
csvBom: false,
fileName: "orders",
maxRows: 1000,
})
}
>
Export selection
</button>
);
}Render the button inside DataGridProvider.
ExportGridOptions
Prop
Type
Without a source, scope: "view" and scope: "all" cover the rows the grid holds. That is all pages of a client grid that pages its view, the current page of server pages, and the loaded rows in scroll mode. scope: "page" covers the shown page. scope: "selection" covers the selected rows. With a source, scope does not apply. See Export: server mode and Export the whole result. csvDelimiter takes a CsvDelimiter: ",", ";", "|", or a tab.
xlsxWriter: "stream" writes an XLSX file slice by slice, through the stream writer of the XLSX library. The file has the same cells, the same cut at 32,767 characters, and the same sheet limits as the default "buffered" writer. It applies to a loaded grid and to a source export.
scope: "checked-rows" covers the checked rows only. scope: "ranges" covers the rows of the cell ranges and ignores checked rows.
Shape and output
columns, formatCell, output, source, maxBytes, signal, and onProgress are fields of ExportGridOptions. formatCell returns an ExportCellValue (string, number, boolean, Date, null, or undefined). exportRows(state, options, onBatch) is the engine of a source export. See Shape the export file.
Types
ExportOutputreceives the finishedBloband{ fileName, format, signal }. It replaces the browser download.ExportCellContextis whatformatCellreceives:value,text,row, andcolumn.FormatExportCellis the type offormatCell.ExportRowsOptionsare the limits and callbacks of an export from a source.ExportProgressis{ rowCount, byteCount }, the argument of the progress callback.ExportResultis{ rowCount, truncated }.truncatedistruewhenmaxRowsormaxBytesended the export.
XLSX export and print cut a cell text at 32,767 characters. CSV export cuts only a string that formatCell returns.
PrintGridOptions
Prop
Type
paperSize takes a PrintPaperSize: "A4" (default), "A3", "Letter", or "Legal". The printed rows are min(maxRows, floor(500,000 / visible columns)). maxRows defaults to 10,000. NaN reads as the default and a negative value counts as 0. Print uses the display text of each cell (toDisplayText, else toText).
DataGridExportButtonProps
DataGridPrintButtonProps
Hook results
| Hook | Result |
|---|---|
useDataGridExport() | UseDataGridExportResult, { exportGrid }. exportGrid(options?) returns Promise<ExportResult>. |
useDataGridPrint() | UseDataGridPrintResult, { printGrid }. printGrid(options?) returns Promise<PrintGridResult>. |
Related
- Export: the scopes, the CSV and XLSX rules, and print.
- Import options: the options of the matching import.
- Clipboard: the cell text that copy and export share.