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.

Excel header and footer added with GemBox.Spreadsheet
Screenshot of Excel header and footer added with GemBox.Spreadsheet
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // 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

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.