Excel header and footer
GemBox.Spreadsheet for Java has been retired
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.

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
Published: December 13, 2018 | Modified: December 19, 2022 | Author: Marek Turis