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 the 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%");
    }
}

See also


Next steps

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

Download Buy