Export to Excel

March 4, 2023 ยท View on GitHub

Blazor server-side

Export to Excel

Index

Grids can be exported to an Excel file. You have to use the SetExcelExport method of the GridClient object:

    var client = new GridClient<Order>(q => orderService.GetOrdersGridRows(columns, q), query, false, "ordersGrid", columns)
        .SetExcelExport(true, false, "Orders"));

The SetExcelExport method has 3 parameters, one of them is required:

ParameterDescription
enabledbool to enable Excel export (required)
allRowsbool to enable export of all grid rows (optional). The default value is false and only visible rows are exported
fileNamestring to define the file name (optional). If no value is defined, the grid internal name is used as file name

You can construct a custom display value of the column only for the exported file using the RenderExcelAs method:

    Columns.Add(o => o.Employees.LastName)
       .RenderExcelAs(o => o.Employees.FirstName + " " + o.Employees.LastName)

This is an example of a grid with an export to Excel button:

Grid columns can be customised as hidden (or not) specifically when exporting to Excel; either by calling SetExcelHidden(bool?) or setting the ExcelHidden property on the column definition.

<- Embedded components on the grid | Grid dimensions ->