Excel headers and footers

Headers and footers in Excel files are rich content that appears on the top and bottom of the worksheet's pages. The content can consist of text, fields (like page number, date, and time), and pictures.

GemBox.Spreadsheet represents worksheet headers and footers with SheetHeaderFooter. Every worksheet can specify HeaderFooterPage that applies to first, default, or even pages. Every header and footer on the page can specify HeaderFooterSection that applies to the left, center, or right section.

The following example shows how you can add headers and footers to the first page and all other pages of an Excel worksheet.

Adding headers and footers on first and default pages of Excel worksheet from C# and VB.NET
Screenshot of Excel headers and footers
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("HeadersFooters");

        SheetHeaderFooter sheetHeadersFooters = worksheet.HeadersFooters;

        HeaderFooterPage firstHeaderFooter = sheetHeadersFooters.FirstPage;
        HeaderFooterPage defaultHeaderFooter = sheetHeadersFooters.DefaultPage;

        // Set title text on the center of the first page header.
        firstHeaderFooter.Header.CenterSection
            .Append("Title on the first page",
                new ExcelFont() { Name = "Arial Black", Size = 18 * 20 });

        // Set image on the left of the first and default page headers.
        firstHeaderFooter.Header.LeftSection
            .AppendPicture("Dices.png", 40, 30);
        defaultHeaderFooter.Header.LeftSection = firstHeaderFooter.Header.LeftSection;

        // Set page number on the right of the first and default page footer.
        firstHeaderFooter.Footer.RightSection
            .Append("Page ")
            .Append(HeaderFooterFieldType.PageNumber)
            .Append(" of ")
            .Append(HeaderFooterFieldType.NumberOfPages);
        defaultHeaderFooter.Footer = firstHeaderFooter.Footer;

        worksheet.Cells[0, 0].Value = "First page";
        worksheet.Cells[0, 5].Value = "Second page";
        worksheet.Cells[0, 10].Value = "Third page";

        worksheet.VerticalPageBreaks.Add(5);
        worksheet.VerticalPageBreaks.Add(10);

        workbook.Save("Headers and Footers.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

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

        Dim workbook As New ExcelFile()
        Dim worksheet = workbook.Worksheets.Add("HeadersFooters")

        Dim sheetHeadersFooters As SheetHeaderFooter = worksheet.HeadersFooters

        Dim firstHeaderFooter As HeaderFooterPage = sheetHeadersFooters.FirstPage
        Dim defaultHeaderFooter As HeaderFooterPage = sheetHeadersFooters.DefaultPage

        ' Set title text on the center of the first page header.
        firstHeaderFooter.Header.CenterSection _
            .Append("Title on the first page",
                New ExcelFont() With {.Name = "Arial Black", .Size = 18 * 20})

        ' Set image on the left of the first and default page headers.
        firstHeaderFooter.Header.LeftSection _
            .AppendPicture("Dices.png", 40, 30)
        defaultHeaderFooter.Header.LeftSection = firstHeaderFooter.Header.LeftSection

        ' Set page number on the right of the first and default page footer.
        firstHeaderFooter.Footer.RightSection _
            .Append("Page ") _
            .Append(HeaderFooterFieldType.PageNumber) _
            .Append(" of ") _
            .Append(HeaderFooterFieldType.NumberOfPages)
        defaultHeaderFooter.Footer = firstHeaderFooter.Footer

        worksheet.Cells(0, 0).Value = "First page"
        worksheet.Cells(0, 5).Value = "Second page"
        worksheet.Cells(0, 10).Value = "Third page"

        worksheet.VerticalPageBreaks.Add(5)
        worksheet.VerticalPageBreaks.Add(10)

        workbook.Save("Headers and Footers.%OutputFileType%")

    End Sub
End Module

Headers and footers can only be seen when the Excel file is exported to PDF, XPS, and image format, or when it's printed, or when its View Options is set to ViewType.PageLayout.

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.