How to: Add Headers and Footers to a Worksheet Printout

  • 3 min to read

Use the Worksheet.HeaderFooterOptions property to add headers and footers to the printed pages.

Properties

Description

WorksheetHeaderFooterOptions.FirstHeader,

WorksheetHeaderFooterOptions.FirstFooter

Specify the header and footer for the first page.

WorksheetHeaderFooterOptions.OddHeader,

WorksheetHeaderFooterOptions.OddFooter

Specify the header and footer for odd pages.

WorksheetHeaderFooterOptions.EvenHeader,

WorksheetHeaderFooterOptions.EvenFooter

Specify the header and footer for even pages.

Property Description
WorksheetHeaderFooterOptions.DifferentOddEven Gets or sets whether odd and even pages have different headers and footers.
WorksheetHeaderFooterOptions.DifferentFirst Gets or sets whether the first page has a unique header and footer.
WorksheetHeaderFooterOptions.ScaleWithDoc Gets or sets whether headers and footers should resize when you scale the worksheet.
WorksheetHeaderFooterOptions.AlignWithMargins Gets or sets whether to align the header and footer margins with the left and right page margins.

The Spreadsheet supports special codes you can use to add dynamic data to a header or footer, and format its content. These codes are also available as constant fields and static methods of the HeaderFooterCode class.

Code

HeaderFooterCode Member

Description

&L

HeaderFooterCode.LeftSection

Aligns text to the left.

&C

HeaderFooterCode.CenterSection

Aligns text to the center.

&R

HeaderFooterCode.RightSection

Aligns text to the right.

&P

HeaderFooterCode.PageNumber

Inserts the current page number.

&P+number

HeaderFooterCode.CustomPageNumber

Inserts the current page number plus the specified number.

&P-number

HeaderFooterCode.CustomPageNumber

Inserts the current page number minus the specified number.

&N

HeaderFooterCode.PageTotal

Inserts the total number of pages in a workbook.

&N+number

HeaderFooterCode.CustomPageTotal

Inserts the total number of pages plus the specified number.

&N-number

HeaderFooterCode.CustomPageTotal

Inserts the total number of pages minus the specified number.

&D

HeaderFooterCode.Date

Inserts the current date.

&T

HeaderFooterCode.Time

Inserts the current time.

&Z

HeaderFooterCode.WorkbookFilePath

Inserts the path to the document.

&F

HeaderFooterCode.WorkbookFileName

Inserts the document's name.

&A

HeaderFooterCode.WorksheetName

Inserts the current worksheet's name.

&G

HeaderFooterCode.Picture

Inserts an image.

&&

HeaderFooterCode.Ampersand

Inserts an ampersand.

&B

HeaderFooterCode.Bold

Turns bold formatting on or off.

&I

HeaderFooterCode.Italic

Turns italic formatting on or off.

&U

HeaderFooterCode.Underline

Turns underline formatting on or off.

&E

HeaderFooterCode.DoubleUnderline

Turns double underline formatting on or off.

&S

HeaderFooterCode.Strikethrough

Turns strikethrough formatting on or off.

&Y

HeaderFooterCode.Subscript

Turns subscript formatting on or off.

&X

HeaderFooterCode.Superscript

Turns superscript formatting on or off.

&"font name,font type"

HeaderFooterCode.Font,

HeaderFooterCode.BodyFont,

HeaderFooterCode.HeadingsFont

Specifies the text font.

&nn

HeaderFooterCode.FontSize

Sets the font size.

Use a two-digit number to specify the font size in points.

&KRRGGBB

HeaderFooterCode.FontColor

Sets the font color.

Use hexadecimal numbers to specify color components.

&KTTSNNN

HeaderFooterCode.FontColor

Applies the theme color to the characters.

TT is the theme color Id, S is either "+" or "-" of the tint/shade value, and NNN is the tint/shade value.

Example

The following example shows how to specify the header and footer for the first page.

SpreadsheetControl_HeaderFooterPrintPreview


WorksheetHeaderFooterOptions options = worksheet.HeaderFooterOptions;
// Specify that the first page has a unique header and footer.
options.DifferentFirst = true;
// Insert the rich formatted text into the header's left section.
options.FirstHeader.Left = string.Format("{0}&BDev{1}AV", HeaderFooterCode.FontColor(4, -50), HeaderFooterCode.FontColor(4,10));
// Insert the sheet name into the header's right section.
options.FirstHeader.Right = "&A";
// Insert the current date into the footer's left section.
options.FirstFooter.Left = "&D";
// Insert the current page number into the footer's right section.
options.FirstFooter.Right = string.Format("Page {0} of {1}", "&P", "&N");