Skip to main content

GridXlExportOptions.CustomizeSheetHeader Property

Allows you to add rows above grid content in the output document.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.2.dll

NuGet Package: DevExpress.Blazor

Declaration

public Action<GridExportCustomizeSheetHeaderFooterEventArgs> CustomizeSheetHeader { get; set; }

Property Value

Type Description
Action<GridExportCustomizeSheetHeaderFooterEventArgs>

A delegate method that customizes the document header area.

Remarks

The grid calls the CustomizeSheetHeader delegate method before the grid starts exporting data. You can write the action handler to perform the following actions:

@using DevExpress.Printing.ExportHelpers
@using DevExpress.Export
@using DevExpress.Export.Xl
@using DevExpress.Drawing

<DxGrid @ref="@Grid" Data="@Data">
    <Columns>
        <DxGridDataColumn FieldName="ContactName" />
        <DxGridDataColumn FieldName="ContactTitle" />
        <DxGridDataColumn FieldName="CompanyName" />
        <DxGridDataColumn FieldName="City" />
        <DxGridDataColumn FieldName="Country" />
        <DxGridDataColumn FieldName="Phone" />
    </Columns>
</DxGrid>
<DxButton Text="Export to XLSX" Click="ExportXlsx_Click" />

@code {
    DxGrid Grid { get; set; }
    IEnumerable<Supplier> Data { get; set; }
    protected override async Task OnInitializedAsync() {
        Data = await NwindDataService.GetSuppliersAsync();
    }
    async Task ExportXlsx_Click() {
        var options = new GridXlExportOptions();
        options.CustomizeSheetFooter = e => {
            // See the CustomizeSheetFooter action description.
        };

        options.CustomizeSheetHeader = e => {
            // Specify cell formatting. 
            var FirstCellFormat = new XlFormattingObject();
            FirstCellFormat.Font = new XlCellFont();
            FirstCellFormat.Font.Size = 24;
            FirstCellFormat.Font.Bold = true;
            FirstCellFormat.Alignment = new XlCellAlignment();
            FirstCellFormat.Alignment.HorizontalAlignment = XlHorizontalAlignment.Center;
            FirstCellFormat.Alignment.VerticalAlignment = XlVerticalAlignment.Center;
            var SecondCellFormat = FirstCellFormat;
            SecondCellFormat.Font.Size = 18;

            // Add an image to the top of the document.
            for (var i = 0; i < 8; i++) {
                e.ExportContext.AddRow();
            }
            var ImageCellRange = new XlCellRange(new XlCellPosition(0, 0), new XlCellPosition(5, 7));
            e.ExportContext.MergeCells(ImageCellRange);   
            var fileName = @"C:\temp\DevExpress-Logo.png";
            using (FileStream outfile = new FileStream(fileName, FileMode.Open))
                e.ExportContext.InsertImage(DXImage.FromStream(outfile), ImageCellRange);

            // Add an empty row.
            e.ExportContext.AddRow();
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(0, 8), new XlCellPosition(5, 8)));

            // Add rows with company contacts.
            var AddressCellName = new CellObject { Value = "Address :", Formatting = FirstCellFormat };
            var AddressCellLocation = new CellObject { Value = "505 N. Brand Blvd Suite 1450", Formatting = SecondCellFormat };
            e.ExportContext.AddRow(new[] { AddressCellName, null, AddressCellLocation });
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(0, 9), new XlCellPosition(1, 10)));
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(2, 9), new XlCellPosition(5, 9)));

            var AddressCellLocation2 = new CellObject { Value = "Glendale CA 91203 USA", Formatting = SecondCellFormat };
            e.ExportContext.AddRow(new[] { null, null, AddressCellLocation2 });
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(2, 10), new XlCellPosition(5, 10)));

            var PhoneCellName = new CellObject { Value = "Phone :", Formatting = FirstCellFormat };
            var PhoneCellLocation = new CellObject { Value = "+ 1 (818) 844-3383", Formatting = SecondCellFormat };
            e.ExportContext.AddRow(new[] { PhoneCellName, null, PhoneCellLocation });
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(0, 11), new XlCellPosition(1, 11)));
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(2, 11), new XlCellPosition(5, 11)));

            var FaxCellName = new CellObject { Value = "Fax :", Formatting = FirstCellFormat };
            var FaxCellLocation = new CellObject { Value = "+ 1 (818) 844-3395", Formatting = SecondCellFormat };
            e.ExportContext.AddRow(new[] { FaxCellName, null, FaxCellLocation });
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(0, 12), new XlCellPosition(1, 12)));
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(2, 12), new XlCellPosition(5, 12)));

            var EmailCellName = new CellObject { Value = "Email :", Formatting = FirstCellFormat };
            var EmailCellLocation = new CellObject { Value = "clientservices@devexpress.com" };
            EmailCellLocation.Hyperlink = "mailto:clientservices@devexpress.com";
            e.ExportContext.AddRow(new[] { EmailCellName, null, EmailCellLocation });
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(0, 13), new XlCellPosition(1, 13)));
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(2, 13), new XlCellPosition(5, 13)));

            // Add an empty row.
            e.ExportContext.AddRow();
            e.ExportContext.MergeCells(new XlCellRange(new XlCellPosition(0, 14), new XlCellPosition(5, 14)));
        };
       await Grid.ExportToXlsxAsync("ExportResult", options);
    }
}

Grid - Custom header and footer in exported document

The CustomizeSheetFooter action allows you to add rows below grid content in the output document.

For more information about data export in the Grid component, refer to the following topic: Export Data in Blazor Grid.

See Also