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 for Java
Screenshot of Excel header and footer added with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Header and Footer");

        SheetHeaderFooter headerFooter = worksheet.getHeadersFooters();

        // Show title only on the first page
        headerFooter.getFirstPage().getHeader().getCenterSection().setContent("Title on the first page");

        // Show logo
        headerFooter.getFirstPage().getHeader().getLeftSection().appendPicture("%#Dices.png%", 40, 40);
        headerFooter.getDefaultPage().getHeader().setLeftSection(headerFooter.getFirstPage().getHeader().getLeftSection());

        // "Page number" of "Number of pages"
        headerFooter.getFirstPage().getFooter().getRightSection().append("Page ").append(HeaderFooterFieldType.PAGE_NUMBER).append(" of ").append(HeaderFooterFieldType.NUMBER_OF_PAGES);
        headerFooter.getDefaultPage().setFooter(headerFooter.getFirstPage().getFooter());

        // Fill Sheet1 with some data
        for (int i = 0; i < 140; i++)
            for (int j = 0; j < 9; j++)
                worksheet.getCell(i, j).setValue(i + j);

        workbook.save("Header and Footer.%OutputFileType%");
    }
}

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 for Java 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.