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 of Excel header and footer added with GemBox.Spreadsheet
Excel header and footer added with GemBox.Spreadsheet
using GemBox.Spreadsheet;

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

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Header and Footer");

        var headerFooter = worksheet.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("%#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++)
                worksheet.Cells[i, j].Value = i + j;

        workbook.Save("Header and Footer.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

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

        Dim workbook = New ExcelFile
        Dim worksheet = workbook.Worksheets.Add("Header and Footer")

        Dim headerFooter = worksheet.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("%#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
                worksheet.Cells(i, j).Value = i + j
            Next
        Next

        workbook.Save("Header and Footer.%OutputFileType%")
    End Sub
End Module

Check next example or download examples from GitHub.