How to: Add Headers and Footers to a Worksheet Printout
- 3 minutes to read
Use the Worksheet.HeaderFooterOptions property to add headers and footers to the printed pages.
Properties | Description |
---|---|
Specify the header and footer for the first page. | |
Specify the header and footer for odd pages. | |
Specify the header and footer for even pages. |
Header and Footer Options
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. |
Header and Footer Codes
The Spreadsheet supports special codes you can use to add dynamic data to a header or footer. These codes are also available as constant fields and static methods of the HeaderFooterCode class.
Note
The ampersand symbol (&) is used in headers and footers to specify format codes. Use two ampersands (&&) to add a single ampersand to the header or footer text.
Code | HeaderFooterCode Member | Description |
---|---|---|
&L | Aligns text to the left. | |
&C | Aligns text to the center. | |
&R | Aligns text to the right. | |
&P | Inserts the current page number. | |
&P+number | Inserts the current page number plus the specified number. | |
&P-number | Inserts the current page number minus the specified number. | |
&N | Inserts the total number of pages in a workbook. | |
&N+number | Inserts the total number of pages plus the specified number. | |
&N-number | Inserts the total number of pages minus the specified number. | |
&D | Inserts the current date. | |
&T | Inserts the current time. | |
&Z | Inserts the path to the document. | |
&F | Inserts the document’s name. | |
&A | Inserts the current worksheet’s name. | |
&G | Inserts an image. | |
&& | Inserts an ampersand. |
Note
The WPF Spreadsheet control does not support format settings for the header and footer text.
Example
The following example shows how to specify the header and footer for the first page.
WorksheetHeaderFooterOptions options = worksheet.HeaderFooterOptions;
// Specify that the first page has a unique header and footer.
options.DifferentFirst = true;
// Insert the sheet name into the header's left section.
options.FirstHeader.Left = "&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");
The image below shows the result.