Headers and footers can define your Excel file while you print it, and they can show page numbers, file names, dates, or any other information you like to have on your printed file. Meanwhile, please note that headers and footers are only visible in Print Preview and Page Layout View. You won’t see them in Normal View worksheets.
This article will show you how to add header and footer to Excel using Java codes from the following aspects.
l Add
image header to Excel
l Add
text footer to Excel
l Add
different header and footer for Odd and Even pages
l Add
different header and footer for first page and other pages
DEPENDENCY
I used a free third-party library
called Free Spire.XLS for Java to finish
operations above. Before running codes, we need to add Spire.Xls.jar to IDEA.
There are two methods to do it. One is that downloading the package of the free
library from the link, finding Spire.Xls.jar in the “lib” folder and then
manually adding it to IDEA. The other is that creating a Maven project in IDEA,
typing the following codes in the pom.xml file and finally clicking the button
“import changes”.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
USING THE CODES
Add image header to Excel
import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageHeader {
public static void main(String[] args) throws IOException {
//Create a workbook and load a file
Workbook workbook = new Workbook();
//Get the first sheet
Worksheet worksheet = workbook.getWorksheets().get(0);
//Load an image from disk
BufferedImage image = ImageIO.read( new File("C:\\Users\\Test1\\Desktop\\Image.png"));
//Set the image header
worksheet.getPageSetup().setLeftHeaderImage(image);
worksheet.getPageSetup().setLeftHeader("&G");
//Set the view mode as layout
worksheet.setViewMode(ViewMode.Layout);
//Save the document to file
workbook.saveToFile("output/ImageHeader.xlsx", ExcelVersion.Version2010);
}
}Output
Add text footer to Excelimport com.spire.xls.*;
public class TextFooter {
public static void main(String[] args) {
//Create a workbook and load a file
Workbook workbook = new Workbook();
//Get the first sheet
Worksheet worksheet = workbook.getWorksheets().get(0);
//Set center footer
worksheet.getPageSetup().setCenterFooter("technical demo");
//Set the view mode as layout
worksheet.setViewMode(ViewMode.Layout);
//Save the document to file
workbook.saveToFile("output/TextFooter.xlsx", ExcelVersion.Version2010);
}
}Output
Add different header and footer for Odd and Even pagesimport com.spire.xls.*;
public class HeaderFooter {
public static void main(String[] args) {
//Create a workbook and load a file
Workbook workbook = new Workbook();
//Get the first sheet
Worksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getCellRange("A1").setText("Page 1");
worksheet.getCellRange("J1").setText("Page 2");
//Set different header footer for Odd and Even pages
worksheet.getPageSetup().setDifferentOddEven((byte)1);
//Set the header footer with font, size, bold and color
worksheet.getPageSetup().setOddHeaderString( "&\"Arial\"&12&B&KFFC000 Odd_Header");
worksheet.getPageSetup().setOddFooterString ( "&\"Arial\"&12&B&KFFC000 Odd_Footer");
worksheet.getPageSetup().setEvenHeaderString ( "&\"Arial\"&12&B&KFF0000 Even_Header");
worksheet.getPageSetup().setEvenFooterString ( "&\"Arial\"&12&B&KFF0000 Even_Footer");
//Set the view mode as layout
worksheet.setViewMode(ViewMode.Layout);
//Save the document to file
workbook.saveToFile("output/DifferentHeaderFooter.xlsx", ExcelVersion.Version2010);
}
}Output
Add different header and footer for first page and other pages
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class HeaderFooter2 {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Insert text in A1 and J1
sheet.getCellRange("A1").setText("page 1");
sheet.getCellRange("J1").setText("page 2");
//Set different first page
sheet.getPageSetup().setDifferentFirst((byte)1);
//Set header string and footer string for the first page
sheet.getPageSetup().setFirstHeaderString("First header");
sheet.getPageSetup().setFirstFooterString("First footer");
//Set header string and footer string for other pages
sheet.getPageSetup().setCenterHeader("Header of other pages");
sheet.getPageSetup().setCenterFooter("Footer of other pages");
//Save the document
workbook.saveToFile("output/DifferentFirstPage.xlsx", FileFormat.Version2016);
}
}Output