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 sample 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.


1using GemBox.Spreadsheet;
2using System;
3using System.IO;
4
5class Sample
6{
7    [STAThread]
8    static void Main(string[] args)
9    {
10        // If using Professional version, put your serial key below.
11        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
12
13        ExcelFile ef = new ExcelFile();
14        ExcelWorksheet ws = ef.Worksheets.Add("Header and Footer");
15
16        string pathToResources = "Resources";
17
18        SheetHeaderFooter headerFooter = ws.HeadersFooters;
19
20        // Show title only on the first page
21        headerFooter.FirstPage.Header.CenterSection.Content = "Title on the first page";
22
23        // Show logo
24        headerFooter.FirstPage.Header.LeftSection.AppendPicture(Path.Combine(pathToResources, "Dices.png"), 40, 40);
25        headerFooter.DefaultPage.Header.LeftSection = headerFooter.FirstPage.Header.LeftSection;
26
27        // "Page number" of "Number of pages"
28        headerFooter.FirstPage.Footer.RightSection.Append("Page ").Append(HeaderFooterFieldType.PageNumber).Append(" of ").Append(HeaderFooterFieldType.NumberOfPages);
29        headerFooter.DefaultPage.Footer = headerFooter.FirstPage.Footer;
30
31        // Fill Sheet1 with some data
32        for (int i = 0; i < 140; i++)
33            for (int j = 0; j < 9; j++)
34                ws.Cells[i, j].Value = i + j;
35
36        ef.Save("Header and Footer.xls");
37    }
38}
1Imports GemBox.Spreadsheet
2Imports System.IO
3
4Module Samples
5
6    Sub Main()
7
8        ' If using Professional version, put your serial key below.
9        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
10
11        Dim ef As ExcelFile = New ExcelFile
12        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Header and Footer")
13
14        Dim pathToResources As String = "Resources"
15
16        Dim headerFooter As SheetHeaderFooter = ws.HeadersFooters
17
18        ' Show title only on the first page
19        headerFooter.FirstPage.Header.CenterSection.Content = "Title on the first page"
20
21        ' Show logo
22        headerFooter.FirstPage.Header.LeftSection.AppendPicture(Path.Combine(pathToResources, "Dices.png"), 40, 40)
23        headerFooter.DefaultPage.Header.LeftSection = headerFooter.FirstPage.Header.LeftSection
24
25        ' "Page number" of "Number of pages"
26        headerFooter.FirstPage.Footer.RightSection.Append("Page ").Append(HeaderFooterFieldType.PageNumber).Append(" of ").Append(HeaderFooterFieldType.NumberOfPages)
27        headerFooter.DefaultPage.Footer = headerFooter.FirstPage.Footer
28
29        ' Fill Sheet1 with some data
30        For i As Integer = 0 To 139
31            For j As Integer = 0 To 8
32                ws.Cells(i, j).Value = i + j
33            Next
34        Next
35
36        ef.Save("Header and Footer.xls")
37
38    End Sub
39
40End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.