Skip to content

Data Grid Excel export

Excel workbooks (.xlsx) from a Data Grid, written in the browser with no dependencies and nothing uploaded:

  • toXlsx(grid, options?): a Promise<Blob> of a workbook with the grid’s view (the same columns, rows, groups, and totals as toCsv).
  • xlsxSheet(grid, options?): the sheet as data, to combine several grids or add sheets of your own.
  • writeXlsx(workbook): writes any workbook of sheets (XlsxWorkbook), for example several xlsxSheet results.

CSV and print are grid methods (toCsv, toCsvBlob, print); PDF is in data-grid/pdf.

import { DataGrid } from '@absoluteui/components/data-grid';
import { toXlsx } from '@absoluteui/components/data-grid/export';
interface Invoice {
id: string;
total: number;
}
const host = document.createElement('div');
host.style.setProperty('block-size', '200px');
document.body.append(host);
const grid = DataGrid.create<Invoice>(host, {
label: 'Invoices',
rows: [{ id: 'INV-1', total: 120 }],
rowId: (r) => r.id,
columns: [
{ id: 'id', header: 'Invoice', field: 'id' },
{ id: 'total', header: 'Total', field: 'total', type: 'currency', currency: 'EUR', excel: { style: 'money' } },
],
});
const blob = await toXlsx(grid, { styles: { money: { bold: true } }, autoFilter: true });
console.log(blob.type, blob.size);
  • Values keep their types: numbers, booleans, dates as Excel dates in the grid’s time zone, currency with a number format. Text is always written as text, so formula-like values cannot run; formulas come only from excel.formula code you write.
  • Named styles (styles, a column’s excel.style and excel.cellStyle(value, row)), number formats, merged column group headers, frozen header rows and start-pinned columns (freeze, default on), outline levels collapsed like the grid, AutoFilter, links (http, https, and mailto only), PNG and JPEG images, and several sheets.
  • Sheet protection stores a SHA-512 hash of the password (never the password) and needs Web Crypto, so a secure context.
  • Grid formulas (from data-grid/formulas) are exported as their values.
  • Invalid options throw a TypeError that names the property (“Invalid Excel export: … must be …”).
  • A protection password without Web Crypto throws an Error.

currencyFormat

An Excel currency format for a currency in a locale: the symbol where the locale puts it, its decimals.

function currencyFormat(currency: string, locale: string): string;

toXlsx

The grid as a one-sheet .xlsx Blob. Several grids or sheets: writeXlsx({ sheets: [xlsxSheet(a), xlsxSheet(b)] }).

function toXlsx<Row>(grid: DataGrid<Row>, options?: DataGridXlsxFileOptions): Promise<Blob>;

xlsxSheet

One sheet of a grid’s rows: the same row and column choices as toCsv, typed values (numbers, booleans, dates as Excel dates in the grid’s time zone, text never as formulas), number formats from the column types, group and tree outlines, frozen header rows and start-pinned columns, merged column group headers, and the columns’ excel options (styles, formulas, links, images, widths).

function xlsxSheet<Row>(grid: DataGrid<Row>, options?: DataGridXlsxOptions): XlsxSheet;

xlsxColumn

Column letters for a 0-based index (A, B, … Z, AA): handy in excel.formula code.

const xlsxColumn: typeof columnName;

DataGridXlsxFileOptions

interface DataGridXlsxFileOptions extends DataGridXlsxOptions {
readonly title?: string;
readonly creator?: string;
}

DataGridXlsxOptions

interface DataGridXlsxOptions extends DataGridExportOptions {
readonly sheetName?: string;
readonly header?: boolean;
readonly columnGroups?: boolean;
readonly styles?: Readonly<Record<string, XlsxStyle>>;
readonly headerStyle?: XlsxStyle;
readonly groupStyle?: XlsxStyle;
readonly totalStyle?: XlsxStyle;
readonly freeze?: boolean;
readonly outline?: boolean;
readonly autoFilter?: boolean;
readonly dateFormat?: string;
readonly dateTimeFormat?: string;
readonly protection?: XlsxProtection;
readonly rightToLeft?: boolean;
}

columnName

A1 column letters for a 0-based index.

function columnName(index: number): string;

protectionHash

ECMA-376 sheet protection hash: SHA-512 over salt and UTF-16LE password, then spinCount rounds over hash and a 32-bit little-endian counter.

function protectionHash(password: string, salt: Uint8Array, spinCount: number): Promise<string>;

writeXlsx

Write a workbook as an .xlsx Blob. Untrusted values are safe: text is escaped and never becomes a formula.

function writeXlsx(workbook: XlsxWorkbook): Promise<Blob>;

cellRef

const cellRef: (row: number, column: number) => string;

XlsxCell

interface XlsxCell {
readonly value: XlsxValue;
readonly style?: XlsxStyle;
readonly formula?: string;
readonly link?: string;
}

XlsxImage

interface XlsxImage {
readonly row: number;
readonly column: number;
readonly bytes: Uint8Array;
readonly type: 'png' | 'jpeg';
readonly width: number;
readonly height: number;
readonly alt?: string;
}

XlsxProtection

interface XlsxProtection {
readonly password?: string;
readonly spinCount?: number;
readonly allow?: readonly ('sort' | 'autoFilter' | 'formatColumns' | 'formatRows' | 'selectLockedCells')[];
}

XlsxRow

interface XlsxRow {
readonly cells: readonly (XlsxCell | null)[];
readonly outlineLevel?: number;
readonly hidden?: boolean;
readonly collapsed?: boolean;
}

XlsxSheet

interface XlsxSheet {
readonly name: string;
readonly widths?: readonly (number | undefined)[];
readonly rows: readonly XlsxRow[];
readonly freeze?: {
readonly rows: number;
readonly columns: number;
};
readonly merges?: readonly {
readonly top: number;
readonly left: number;
readonly bottom: number;
readonly right: number;
}[];
readonly autoFilter?: {
readonly top: number;
readonly left: number;
readonly bottom: number;
readonly right: number;
};
readonly images?: readonly XlsxImage[];
readonly protection?: XlsxProtection;
readonly rightToLeft?: boolean;
readonly summaryAbove?: boolean;
}

XlsxStyle

A cell style. Colors are #RRGGBB.

interface XlsxStyle {
readonly bold?: boolean;
readonly italic?: boolean;
readonly underline?: boolean;
readonly color?: string;
readonly fill?: string;
readonly fontSize?: number;
readonly border?: XlsxBorder;
readonly borderTop?: XlsxBorder;
readonly borderBottom?: XlsxBorder;
readonly borderColor?: string;
readonly align?: 'start' | 'center' | 'end';
readonly verticalAlign?: 'top' | 'center' | 'bottom';
readonly wrap?: boolean;
readonly indent?: number;
readonly numberFormat?: string;
}

XlsxWorkbook

interface XlsxWorkbook {
readonly sheets: readonly XlsxSheet[];
readonly creator?: string;
readonly title?: string;
}

XlsxBorder

type XlsxBorder = 'thin' | 'medium' | 'thick';

XlsxValue

type XlsxValue = string | number | boolean | null;