Home » excel » How to set a header on excel page using apache POI?

How to set a header on excel page using apache POI?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a header which I need to set on every page that is printed on a dynamic excel sheet.

Is there any way to get the page number ?

How to&Answers:

As from swamy’s comment the solution is to get the HSSFHeader from the HSSFSheet

HSSFHeader header = sheet.getHeader();

Then you can set left, center and right text including font, font style, font size, page number, date, time etc.

Example

header.setCenter(HSSFHeader.font("Calibri", "regular") + 
       HSSFHeader.fontSize((short) 14) + "My " + HSSFHeader.startBold() + "Styled" + 
       HSSFHeader.endBold() + " Text with page number " + HSSFHeader.page());

Result

enter image description here