# DataGrid Export

Source: https://lumeo.nativ.sh/docs/services/datagrid-export

# DataGrid Export

Export tabular data as CSV, Excel (.xlsx) or PDF (A4) from C#. Works with any IEnumerable — not just the DataGrid component.

## Quick Start

`IDataGridExportService` is registered automatically by `AddLumeo()`. Inject it, describe your columns once, then call `ToCsv` / `ToExcel` / `ToPdf` to get the bytes, and `DownloadAsync` to stream them to the browser.

@inject IDataGridExportService Export var columns = new\[\] { new DataGridExportColumn<Order>("Id", o => o.Id), new DataGridExportColumn<Order>("Date", o => o.Date, "yyyy-MM-dd"), new DataGridExportColumn<Order>("Amount", o => o.Amount, "C2"), }; var bytes = Export.ToExcel(orders, columns, sheetName: "Orders"); await Export.DownloadAsync(bytes, "orders.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

PDF export is powered by `QuestPDF` under its Community license. If your company has more than $1M in annual revenue you must purchase a QuestPDF Professional license and set `QuestPDF.Settings.License = LicenseType.Professional;` once at app startup.

## Live Demo

A small product catalogue. Click any export button to download the full table.

Export CSV Export Excel Export PDF

The `Export PDF` button is disabled here because QuestPDF's renderer needs a native Skia dependency that isn't available under browser-wasm. CSV and Excel work on every runtime.

SKU

Name

Category

Price

Stock

Added

SKU-001

Logitech MX Master 3S

Electronics

¤99.00

42

2025-10-12

SKU-002

Herman Miller Aeron

Furniture

¤1,395.00

7

2025-11-03

SKU-003

Moleskine Classic

Stationery

¤24.50

200

2026-01-08

SKU-004

Nespresso Vertuo

Appliances

¤199.00

18

2026-02-14

SKU-005

Sony WH-1000XM5

Electronics

¤349.99

33

2026-03-22

## API Reference

### IDataGridExportService

Method

Returns

Description

ToCsv<T>(items, columns)

byte\[\]

UTF-8 CSV with BOM. Escapes commas, quotes, newlines.

ToExcel<T>(items, columns, sheetName)

byte\[\]

.xlsx with bolded header row, auto-fit columns, frozen first row.

ToPdf<T>(items, columns, title)

byte\[\]

Portrait A4 with title, timestamp and page numbers.

DownloadAsync(bytes, fileName, mimeType)

Task

Triggers a browser download via JS interop.

### DataGridExportColumn<T> Record

Property

Type

Description

Header

string

Text shown in the header row for CSV / Excel / PDF.

Accessor

Func<T, object?>

Returns the raw cell value for a given row.

Format

string?

Optional format string applied to IFormattable values (e.g. "N2", "yyyy-MM-dd", "C2").

### Lazy loading (Blazor WebAssembly) — since v3.7.0

The Excel (ClosedXML) and PDF (QuestPDF) backends compile into a separate `Lumeo.DataGrid.Export.dll` that ships inside the `Lumeo.DataGrid` NuGet package. On Blazor WebAssembly you can lazy-load it on demand, keeping ~1.65 MB (brotli) of dependencies out of the initial download. Server scenarios are unaffected — the backend always loads eagerly there.

#### 1\. Mark the assemblies lazy

In your WASM app's `.csproj`:

<ItemGroup> <BlazorWebAssemblyLazyLoad Include="Lumeo.DataGrid.Export.dll" /> <BlazorWebAssemblyLazyLoad Include="ClosedXML.dll" /> <BlazorWebAssemblyLazyLoad Include="ClosedXML.Parser.dll" /> <BlazorWebAssemblyLazyLoad Include="DocumentFormat.OpenXml.dll" /> <BlazorWebAssemblyLazyLoad Include="DocumentFormat.OpenXml.Framework.dll" /> <BlazorWebAssemblyLazyLoad Include="ExcelNumberFormat.dll" /> <BlazorWebAssemblyLazyLoad Include="RBush.dll" /> <BlazorWebAssemblyLazyLoad Include="SixLabors.Fonts.dll" /> <BlazorWebAssemblyLazyLoad Include="System.IO.Packaging.dll" /> <BlazorWebAssemblyLazyLoad Include="QuestPDF.dll" /> </ItemGroup>

#### 2\. Wire the loader hook

In `Program.cs`:

using Lumeo.Services; using Microsoft.AspNetCore.Components.WebAssembly.Services; var host = builder.Build(); var lazy = host.Services.GetRequiredService<LazyAssemblyLoader>(); DataGridExportLoader.LoadAssembliesAsync = names => lazy.LoadAssembliesAsync(names); await host.RunAsync();

The DataGrid component awaits the loader before its toolbar export, so Excel pulls the ClosedXML closure and PDF pulls QuestPDF on the first click — an Excel-only WASM user never downloads QuestPDF. If you call `IDataGridExportService.ToExcel/ToPdf` **directly** (not via the DataGrid toolbar), first await `DataGridExportLoader.EnsureExcelAssembliesAsync()` / `EnsurePdfAssembliesAsync()` — the synchronous service methods can't fetch a lazy assembly themselves. Without this opt-in nothing breaks: the backend loads eagerly at startup like before.

#### 3\. Direct service calls (optional)

When calling the service yourself on a lazy-loading WASM app, preload first:

await DataGridExportLoader.EnsureExcelAssembliesAsync(); var bytes = Export.ToExcel(rows, columns, sheetName: "Data"); await Export.DownloadAsync(bytes, "data.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
