Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

Downloads

A grid's rows download as Excel, CSV or PDF from endpoints you map. A chart downloads as a PNG that the browser creates. To put a chart in a PDF, send its picture to your server in a form.

Exporting a grid's rows

To add an Export button to a DataGrid or TreeList, set ExportExcelHref or ExportCsvHref to an endpoint you map. The link contains the grid's filter, sort, search and columns. On the server, DataExport.WriteExcelAsync and WriteCsvAsync write those rows, and stream them as they're read:

Program.cs (server)
app.MapGet("/orders/export.xlsx", (HttpContext context, ShopDb db) => Results.Stream(
    stream => DataExport.WriteExcelAsync(stream, db.Orders.AsNoTracking(), context.Request.QueryString.Value,
        DataFields.Of<Order>(), "Orders", cancellationToken: context.RequestAborted),
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "orders.xlsx"));
  • The file contains the rows the user sees, from every page, and the columns shown, in their order.
  • It's a link, so it works on static pages and with JavaScript off.
  • The fields you pass to DataExport set what can be written, filtered and searched, whatever the URL asks for. DataFields.Of<Order>() is every property of the type: when it has some a download shouldn't hold, pass only the others (DataFields.Of<Order>().Where(f => f.Key != "internalnote").ToList()). See DataGrid export.
  • The fields need the grid's keys. If the grid sets FilterCase, set the same value as each field's Case. Otherwise the file can contain different rows.
  • For a TreeList, pass tree: to write the rows in the tree's order, with their levels.

Other formats

For a format the library doesn't write, DataExport.Read returns the same columns and rows that the Excel and CSV writers get, and you write the file. This PDF endpoint uses QuestPDF (opens in a new tab), but any PDF library works the same way. To list the format on the grid, add <DataGridExportFormat Label="Export PDF" Href="/orders/export.pdf" />.

Program.cs (server)
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;

QuestPDF.Settings.License = LicenseType.Community;

app.MapGet("/orders/export.pdf", async (HttpContext context, ShopDb db) =>
{
    var orders = db.Orders.AsNoTracking()
        .Select(o => new OrderRow { Id = o.Id, Customer = o.Customer.Name, Total = o.Total });
    var export = DataExport.Read(orders, context.Request.QueryString.Value,
        DataFields.Of<OrderRow>(), context.RequestAborted);
    var rows = new List<object?[]>();
    await foreach (var row in export.Rows)
    {
        if (rows.Count == 5_000)
        {
            return Results.Problem("Narrow the filter: a PDF holds at most 5,000 orders.", statusCode: 413);
        }

        rows.Add(row);
    }

    var pdf = Document.Create(document => document.Page(page =>
    {
        page.Margin(36);
        page.Content().Table(table =>
        {
            table.ColumnsDefinition(columns => export.Columns.ToList().ForEach(_ => columns.RelativeColumn()));
            table.Header(header => export.Columns.ToList().ForEach(c => header.Cell().Text(c.Caption).Bold()));
            foreach (var row in rows)
            {
                for (var i = 0; i < row.Length; i++)
                {
                    table.Cell().PaddingVertical(2).Text(export.Columns[i].Text(row[i]));
                }
            }
        });
    })).GeneratePdf();
    return Results.File(pdf, "application/pdf", "orders.pdf");
});
  • A PDF is laid out all at once, so this endpoint reads every row first. Where a filter can match millions of rows, cap the rows as this endpoint does.
  • The query runs when the rows are read. An endpoint that streams the file, as CSV does, reads the first row before it starts the response. So if the query fails, the endpoint can still return an error.
  • Each column's Key is its name in a keyed format such as JSON. Its Text formats a value the way CSV does.
  • QuestPDF is free under its Community license for organizations below $1M in revenue, and paid above it.
  • For rows read by DynamicColumn, build the fields with DataFields.ByName, by the same names and types, so the keys and values match the grid's: DataFields.ByName<IDictionary<string, object?>>("Margin %", typeof(double)).

Saving a chart as a PNG

To add a button that saves a chart as a PNG, set Download. The button appears by the chart's title, and the file is named after the title. In a Tile's or Card's header, <ChartDownload /> adds the same button for the chart in it, or for the chart its For names. To use a button of your own, set its data-ib-chart-download to the chart's id:

razor
<Chart Items="quarters" Argument="q => q.Quarter" Title="Revenue by region, 2026" Download>
    ...
</Chart>

<Button StartIcon="Lucide.Download" data-ib-chart-download="signups">Download</Button>
<Chart id="signups" Items="days" Argument="d => d.Day" Title="New signups, last 7 days">
    ...
</Chart>

The browser creates the picture at twice the chart's size on screen, in the page's current color scheme. It works on static pages too. With JavaScript off, there's no picture. See Chart download.

Putting a chart in a PDF

To put a chart in a PDF your server builds, send its picture in a form. Add a file input and set its data-ib-chart-png to the chart's id. When the form is sent, the input receives the chart's PNG, and the endpoint reads it like any uploaded file:

Revenue.razor
<form method="post" action="/reports/revenue.pdf">
    <AntiforgeryToken />
    <input type="file" name="chart" data-ib-chart-png="revenue" hidden />
    <Button Submit StartIcon="Lucide.FileDown">Download PDF</Button>
</form>

<Chart id="revenue" Items="quarters" Argument="q => q.Quarter" Title="Revenue by region, 2026">
    ...
</Chart>
Program.cs (server)
using QuestPDF.Fluent;

app.MapPost("/reports/revenue.pdf", async (IFormFile? chart) =>
{
    if (chart is null)
    {
        return Results.BadRequest("The chart's picture didn't come with the form.");
    }

    using var image = new MemoryStream();
    await chart.CopyToAsync(image);
    var pdf = Document.Create(document => document.Page(page =>
    {
        page.Margin(36);
        page.Header().Text("Revenue report, 2026").FontSize(18).Bold();
        page.Content().PaddingTop(12).Image(image.ToArray());
    })).GeneratePdf();
    return Results.File(pdf, "application/pdf", "revenue.pdf");
});
  • The form is sent as multipart/form-data, which a file requires, whatever its enctype is set to.
  • The form's other fields are sent too, such as a date range or a note for the report. For several charts, add an input for each.
  • Leave out data-enhance, because the response is a file, not a page.
  • With JavaScript off, or when the chart couldn't be drawn, the input is empty and the endpoint receives no file. This endpoint returns an error, but it could build the PDF without the chart instead.

Printing

The browser's Print command prints the page. Its Save as PDF option creates a PDF with no code on the server. Charts and tables print as they appear on screen.

Connection lost

Rejoining the server…