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 (such as 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 to 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, or image formats, when it's printed, or when View Options is set to ViewType.PageLayout.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy