Introduction
Headers or footers are invisible on the worksheet in Normal view. They are only displayed in Page Layout view or the printed pages. We can add the headers or footers at the top or bottom of a printed worksheet to display page number, worksheet name, current data, text or a picture.
In this article, I am going to introduce how to programmatically add the headers or footers to an Excel sheet from the three aspects, given below-
- Add formatted text to the headers or footers.
- Add image to the headers or footers.
- Set different header or footer for odd and even pages.
Background
This solution requires a .NET Excel component, which provides a PageSetup class to deal with all page setup settings. Specifically, it contains LeftHeader, CenterHeader, LeftHeaderImage, OddHeaderString and similar properties to represent header/footer text, image, odd header/footer and even header/footer in Excel. Besides, this component provides some special script commands, which allows you to set header and footer formatting.
| Script | Description |
| &P | The current page number |
| &N | The total number of pages |
| &D | The current data |
| &T | The current time |
| &G | A picture |
| &A | The worksheet name |
| &F | The file name |
| &B | Make text bold |
| &I | Italicize text |
| &U | Underline text |
| &”font name” | Represents a font name, for example, &”Aril” |
| &font size | Represents font size, for example, &12 |
| &K<HEX color> | Represents font color, for example, &KFF0000 |



Kenneth StanleyPosted Nov 19, 2021, 8:52 PM
By far the best information that I have found on this topic on the internet.
Filippo GramaiPosted Oct 11, 2017, 1:06 PM
It’s possibile do the same things with file Microsoft Project? Thanks, Filippo
Leo GurdianPosted Oct 4, 2017, 7:40 PM
Very helpful! thank you for sharing.
Humayun Kabir MamunPosted Oct 13, 2016, 4:31 AM
Nice...
SubashPosted Oct 11, 2016, 7:53 AM
Nice