Excel Header and Footer

Every Excel worksheet can contain headers and / or footers which can be defined on the first page, even pages and all other pages on which worksheet is shown / printed.

Following example shows how to add headers and footers to the first page and to all other pages of a worksheet.

Screenshot
Excel Header and Footer Screenshot

See the full code below, use Run Example to execute.

using GemBox.Spreadsheet;
using System;
using System.IO;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        ExcelFile ef = new ExcelFile();
        ExcelWorksheet ws = ef.Worksheets.Add("Header and Footer");

        string pathToResources = "Resources";

        SheetHeaderFooter headerFooter = ws.HeadersFooters;

        // Show title only on the first page
        headerFooter.FirstPage.Header.CenterSection.Content = "Title on the first page";

        // Show logo
        headerFooter.FirstPage.Header.LeftSection.AppendPicture(Path.Combine(pathToResources, "Dices.png"), 40, 40);
        headerFooter.DefaultPage.Header.LeftSection = headerFooter.FirstPage.Header.LeftSection;

        // "Page number" of "Number of pages"
        headerFooter.FirstPage.Footer.RightSection.Append("Page ").Append(HeaderFooterFieldType.PageNumber).Append(" of ").Append(HeaderFooterFieldType.NumberOfPages);
        headerFooter.DefaultPage.Footer = headerFooter.FirstPage.Footer;

        // Fill Sheet1 with some data
        for (int i = 0; i < 140; i++)
            for (int j = 0; j < 9; j++)
                ws.Cells[i, j].Value = i + j;

        ef.Save("Header and Footer.xlsx");
    }
}
Imports GemBox.Spreadsheet
Imports System.IO

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim ef As ExcelFile = New ExcelFile
        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Header and Footer")

        Dim pathToResources As String = "Resources"

        Dim headerFooter As SheetHeaderFooter = ws.HeadersFooters

        ' Show title only on the first page
        headerFooter.FirstPage.Header.CenterSection.Content = "Title on the first page"

        ' Show logo
        headerFooter.FirstPage.Header.LeftSection.AppendPicture(Path.Combine(pathToResources, "Dices.png"), 40, 40)
        headerFooter.DefaultPage.Header.LeftSection = headerFooter.FirstPage.Header.LeftSection

        ' "Page number" of "Number of pages"
        headerFooter.FirstPage.Footer.RightSection.Append("Page ").Append(HeaderFooterFieldType.PageNumber).Append(" of ").Append(HeaderFooterFieldType.NumberOfPages)
        headerFooter.DefaultPage.Footer = headerFooter.FirstPage.Footer

        ' Fill Sheet1 with some data
        For i As Integer = 0 To 139
            For j As Integer = 0 To 8
                ws.Cells(i, j).Value = i + j
            Next
        Next

        ef.Save("Header and Footer.xlsx")

    End Sub

End Module