Skip to main content
A newer version of this page is available. .

How to: Add Headers and Footers to a Worksheet Printout

  • 3 minutes to read

The example below demonstrates how to insert headers and footers at the top and bottom of a worksheet printout.

To define the header and footer options, use the Worksheet.HeaderFooterOptions property. It provides access to the WorksheetHeaderFooterOptions object, which allows you to specify the header or footer for the first page (WorksheetHeaderFooterOptions.FirstHeader and WorksheetHeaderFooterOptions.FirstFooter), odd-numbered pages (WorksheetHeaderFooterOptions.OddHeader and WorksheetHeaderFooterOptions.OddFooter) and even-numbered pages (WorksheetHeaderFooterOptions.EvenHeader and WorksheetHeaderFooterOptions.EvenFooter) of the printed worksheet.

Use the properties of the WorksheetHeaderFooterOptions object to specify the following header/footer settings.

The SpreadsheetControl supports specific codes that enable including dynamic information into a header or footer, such as a page number, current date and time, filename, worksheet name, and so on. Note that these codes can also be obtained as constant fields of the HeaderFooterCode class.

The supported header/footer codes are listed in the table below.

Code HeaderFooterCode member Description
&L HeaderFooterCode.LeftSection Left aligns the characters that follow.
&C HeaderFooterCode.CenterSection Centers the characters that follow.
&R HeaderFooterCode.RightSection Right aligns the characters that follow.
&P HeaderFooterCode.PageNumber Inserts the current page number.
&N HeaderFooterCode.PageTotal Inserts the total number of pages in a workbook.
&D HeaderFooterCode.Date Inserts the current date.
&T HeaderFooterCode.Time Inserts the current time.
&Z HeaderFooterCode.WorkbookFilePath Inserts the workbook file path.
&F HeaderFooterCode.WorkbookFileName Inserts the name of a workbook file.
&A HeaderFooterCode.WorksheetName Inserts the name of a worksheet.
&G HeaderFooterCode.Picture Inserts an image.
&& HeaderFooterCode.Ampersand Inserts the ampersand character.

Note

Currently, the SpreadsheetControl does not support formatting codes for the header or footer text.

Example

The following example prints the workbook file name in the first page header, and inserts the current date and the page indicator in the first page footer.


WorksheetHeaderFooterOptions options = worksheet.HeaderFooterOptions;
// Specify that the first page of the worksheet has unique headers and footers. 
options.DifferentFirst = true;
// Set headers and footers for the first page.
// Insert the workbook name into the header left section.
options.FirstHeader.Left = "&F";
// Insert the current date into the footer left section.
options.FirstFooter.Left = "&D";
// Insert the current page number into the footer right section.
options.FirstFooter.Right = string.Format("Page {0} of {1}", "&P", "&N");

The image below shows the result.

SpreadsheetControl_HeaderFooterPrintPreview

See Also