How to Insert Headers and Footers in Excel via Java
Headers and footers are commonly used in Excel documents to display supplementary information such as dates, company names, logos, or author details. This article demonstrates how to programmatically add headers and footers to Excel files using Java, covering uniform layouts, distinct odd/even pages, and unique first-page setups
Prerequisites
Before running the code, you need to add the required JAR library to your IDE. You can either download the Free Spire.XLS for Java package from the official website, extract it, and manually import Spire.Xls.jar from the lib folder; or (recommended) install it directly via a Maven repository. For Maven projects, simply add the following configuration to your pom.xml file and click "Import Changes":
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
Adding Text and Image Headers/Footers
Free Spire.XLS for Java supports adding both text and images to headers and footers. You can precisely control the alignment (left, center, right) as well as the text font, size, and color.
Note: Excel headers and footers are invisible in the Normal view mode. They only appear in Page Layout mode or Print Preview mode.
import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class HeaderFooter {
public static void main(String[] args) throws IOException {
// Load the sample Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("Test.xlsx");
// Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Add text to header/footer and configure font, size, color, and alignment
worksheet.getPageSetup().setRightHeader("&\"Microsoft YaHei\"&14 Tech Corp Ltd.");
worksheet.getPageSetup().setRightFooter("&\"Microsoft YaHei\"&12&B&KFF0000 Note: Internal Staff Only");
// Load the image
BufferedImage image = ImageIO.read(new File("Image.png"));
// Add the image to the header and set alignment
worksheet.getPageSetup().setLeftHeaderImage(image);
worksheet.getPageSetup().setLeftHeader("&G"); // &G triggers the image rendering
// Add the image to the footer (Optional)
// worksheet.getPageSetup().setCenterFooterImage(image);
// worksheet.getPageSetup().setCenterFooter("&G");
// Set the view mode to Page Layout to make headers/footers visible
worksheet.setViewMode(ViewMode.Layout);
// Save the document
workbook.saveToFile("output/HeaderFooter.xlsx", ExcelVersion.Version2010);
}
}
Different Headers/Footers for Odd and Even Pages
Free Spire.XLS for Java provides the setDifferentOddEven method to apply separate headers and footers for odd and even pages. Text formatting like font, size, and color can also be customized simultaneously.
import com.spire.xls.*;
public class DifferentHeaderFooter {
public static void main(String[] args) {
// Load the sample Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.xlsx");
// Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Set up content across multiple pages for demonstration
worksheet.getCellRange("A1").setText("Page 1");
worksheet.getCellRange("M1").setText("Page 2");
// Set setDifferentOddEven to 1, enabling separate headers/footers for odd and even pages
worksheet.getPageSetup().setDifferentOddEven((byte)1);
// Configure headers, footers, styles, and colors for odd and even pages
worksheet.getPageSetup().setOddHeaderString("&\"Arial\"&20&B&K191970 Odd_Header");
worksheet.getPageSetup().setOddFooterString("&\"Arial\"&20&B&K191970 Odd_Footer");
worksheet.getPageSetup().setEvenHeaderString("&\"Arial\"&20&B&KFF0000 Even_Header");
worksheet.getPageSetup().setEvenFooterString("&\"Arial\"&20&B&KFF0000 Even_Footer");
// Set the view mode
worksheet.setViewMode(ViewMode.Layout);
// Save the document
workbook.saveToFile("output/DifferentHeaderFooter.xlsx", ExcelVersion.Version2010);
}
}
Different Headers/Footers for the First Page and Subsequent Pages
If you want to apply a unique style to the cover or first page while keeping a consistent style for the rest of the document, use the setDifferentFirst method.
import com.spire.xls.*;
public class DifferentHeaderFooterOnFirstPage {
public static void main(String[] args) {
// Load the sample Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.xlsx");
// Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Set up content across multiple pages
worksheet.getCellRange("A1").setText("Page 1");
worksheet.getCellRange("M1").setText("Page 2");
// Set setDifferentFirst to 1, allowing separate configuration for the first page
worksheet.getPageSetup().setDifferentFirst((byte)1);
// Configure header, footer, styles, and colors for the first page
worksheet.getPageSetup().setFirstHeaderString("&\"Arial\"&B&20&KFF0000 FirstPage_Header");
worksheet.getPageSetup().setFirstFooterString("&\"Arial\"&B&20&KFF0000 FirstPage_Footer");
// Configure header, footer, styles, and colors for subsequent pages
worksheet.getPageSetup().setCenterHeader("&\"Arial\"&B&20&K191970 OtherPages_Header");
worksheet.getPageSetup().setCenterFooter("&\"Arial\"&B&20&K191970 OtherPages_Footer");
// Save the document
workbook.saveToFile("output/DifferentHeaderFooterOnFirstPage.xlsx", ExcelVersion.Version2013);
}
}
Conclusion
With the above code examples, you can easily integrate custom headers and footers into your Excel workbooks. Additionally, for more tailored document requirements, the API seamlessly supports variations like odd/even page differentiation, unique first-page templates, and advanced text formatting (fonts, sizes, colors, etc.).