Setting Headers and Footers

Setting Headers and Footers

Aspose.Cells allows you to add headers and footers to worksheets at runtime but we recommend setting headers and footers manually in a pre-designed file for printing. You can use Microsoft Excel as a GUI tool to set headers and footers to save effort and development time. Aspose.Cells can import the file and save the settings.

To add headers and footers at runtime, Aspose.Cells provides special API calls and script commands to format headers and footers.

Script Commands

Script commands are special commands that allow you to set header and footer formatting.

Script Commands Description
&P The current page number
&G A picture
&N The total number of pages
&D The current date
&T The current time
&A The worksheet name
&F The file name without its path
&"<FontName>" Represents a font name. For example: &“Arial”
&"<FontName>, <FontStyle>" Represents font name with style. For example: &“Arial,Bold”
&<FontSize> Represents font size. For example: “&14abc”. But, if this command is followed by a plain number to be printed in the header, this should be separated with a space character from the font size. For example: “&14 123”.

Set Headers and Footers

The PageSetup class provides two methods, SetHeader and SetFooter, used to add a header and footer to a worksheet. These methods take only two parameters:

  • Section – the section where the header or footer should be placed. There are three sections: left, center and right, represented by 0, 1 and 2 respectively.
  • Script – the script to be used for the header or footer. This script contains script commands to format headers or footers.

The PageSetup class has two additional methods, SetHeaderPicture and SetFooterPicture, used to add pictures into the header and footer. These methods take the parameters:

  • Section – the header or footer section where the picture will be placed. There are three sections, left, center and right, represented by the values 0, 1 and 2 respectively.
  • Byte array – the graphical data (the binary data should be written into the buffer of a byte array).

After executing the code below and opening the file, check the header of the worksheet by:

  1. On the File menu, select Page Setup. A dialog will be displayed.
  2. Select the Header/Footer tab.