New to Telerik UI for WinForms? Download free 30-day trial

Add Header and Footer to the Exported Document

This article will show how you can add header and footer to your exported document. After the document is exported it will look like in figure 1.

Fig.1 The final exported document.

WinForms RadGridView Final Exported Document

The spread export functionality gives you access to the exported document. It can be accessed in the WorkbookCreated event. The following steps are showing how you can use this event to add header and footer.

1. You can use the following code to initialize the exporter and subscribe to the event.

Initialize the exporter

GridViewSpreadExport spreadExporter = new GridViewSpreadExport(radGridView1);
SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
exportRenderer.WorkbookCreated += exportRenderer_WorkbookCreated;
spreadExporter.RunExport(@"C:\exportedFile.xlsx", exportRenderer);

2. Before adding the header you should declare the two elements which will be used later for the cell value format and the background color.

Define styles and formats

PatternFill solidPatternFill = new PatternFill(PatternType.Solid, System.Windows.Media.Color.FromRgb(46, 204, 113), Colors.Transparent);
CellValueFormat textFormat = new CellValueFormat("@");

You need to add a reference to the PresentationCore assembly and the System.Windows.Media namespace.

3. Now you can add the header, first you need to insert a new row on top of the document. Then you can merge the all the cells above the grid and set the new cell value and styles.

Add header

Worksheet worksheet = e.Workbook.Sheets[0] as Worksheet;
CellRange range = new CellRange(0, 0, 1, radGridView1.Columns.Count - 4);
CellSelection header = worksheet.Cells[range];
if (header.CanInsertOrRemove(range, ShiftType.Down))
{
    header.Insert(InsertShiftType.Down);
}
header.Merge();
header.SetFormat(textFormat);
header.SetHorizontalAlignment(RadHorizontalAlignment.Center);
header.SetVerticalAlignment(RadVerticalAlignment.Center);
header.SetFontFamily(new ThemableFontFamily("Rockwell"));
header.SetFontSize(24);
header.SetFill(solidPatternFill);
header.SetValue("Nortwind Products Details");

4. The final part is adding the footer. For example you can select the left most and right most cells below the actual grid data and set their styles and value. At the end you can set the fill for the entire row.

Add footer

CellSelection footerLeft = worksheet.Cells[radGridView1.RowCount + 4, 0];
footerLeft.SetFormat(textFormat);
footerLeft.SetValue("Nortwind 2015");
footerLeft.SetVerticalAlignment(RadVerticalAlignment.Center);
footerLeft.SetFontFamily(new ThemableFontFamily("Rockwell"));
footerLeft.SetFontSize(24);
CellSelection footerRight = worksheet.Cells[radGridView1.RowCount + 4, radGridView1.Columns.Count - 1];
footerRight.SetFormat(textFormat);
footerRight.SetValue(DateTime.Now.ToShortDateString());
footerRight.SetVerticalAlignment(RadVerticalAlignment.Center);
footerRight.SetFontFamily(new ThemableFontFamily("Rockwell"));
footerRight.SetFontSize(24);
range = new CellRange(radGridView1.RowCount + 4, 0, radGridView1.RowCount + 4, radGridView1.Columns.Count - 1);
CellSelection footer = worksheet.Cells[range];
footer.SetFill(solidPatternFill);
worksheet.Columns[worksheet.UsedCellRange].SetWidth(new ColumnWidth(130, false));
In this article