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
&&Text Shows &Text. For example: &&WO will be displayed as &WO
&"<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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook excel = new Workbook();
// Obtaining the reference of the PageSetup of the worksheet
PageSetup pageSetup = excel.Worksheets[0].PageSetup;
// Setting worksheet name at the left section of the header
pageSetup.SetHeader(0, "&A");
// Setting current date and current time at the centeral section of the header
// and changing the font of the header
pageSetup.SetHeader(1, "&\"Times New Roman,Bold\"&D-&T");
// Setting current file name at the right section of the header and changing the
// font of the header
pageSetup.SetHeader(2, "&\"Times New Roman,Bold\"&12&F");
// Setting a string at the left section of the footer and changing the font
// of a part of this string ("123")
pageSetup.SetFooter(0, "Hello World! &\"Courier New\"&14 123");
// Setting the current page number at the central section of the footer
pageSetup.SetFooter(1, "&P");
// Setting page count at the right section of footer
pageSetup.SetFooter(2, "&N");
// Save the Workbook.
excel.Save("SetHeadersAndFooters_out.xls");

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.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a Workbook object
Workbook workbook = new Workbook();
// Creating a string variable to store the url of the logo/picture
string logo_url = dataDir + "aspose-logo.jpg";
// Declaring a FileStream object
FileStream inFile;
// Declaring a byte array
byte[] binaryData;
// Creating the instance of the FileStream object to open the logo/picture in the stream
inFile = new System.IO.FileStream(logo_url, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// Instantiating the byte array of FileStream object's size
binaryData = new Byte[inFile.Length];
// Reads a block of bytes from the stream and writes data in a given buffer of byte array.
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
// Creating a PageSetup object to get the page settings of the first worksheet of the workbook
PageSetup pageSetup = workbook.Worksheets[0].PageSetup;
// Setting the logo/picture in the central section of the page header
pageSetup.SetHeaderPicture(1, binaryData);
// Setting the script for the logo/picture
pageSetup.SetHeader(1, "&G");
// Setting the Sheet's name in the right section of the page header with the script
pageSetup.SetHeader(2, "&A");
// Saving the workbook
workbook.Save(dataDir + "InsertImageInHeaderFooter_out.xls");
//Closing the FileStream object
inFile.Close();