Home » excel » java – Apache poi HSSFWorkbook overwrites styles from the XLS template

java – Apache poi HSSFWorkbook overwrites styles from the XLS template

Posted by: admin May 14, 2020 Leave a comment


I have a problem with HSSFWorkbook object. I have a XLS template, where first row is dark gray, and next rows are light gray. During generation of the report, I want to change the background color of few cells (works perfectly) for red/blue. But after calling:

HSSFCellStyle style = workbook.createCellStyle();


HSSFCellStyle style = workbook.getCellStyleAt(0) [default style of the workbook]

the styles of whole sheet is lost. The header becomes violet, and the rest cells becomes dark-dark-gray. I love violets, but I need my colours from the XLS not being changed.

Colours problem

In short – styles from my XLS template are not being preserved after I change style of one cell in the way described above.

I’m out of ideas. Please help.


How to&Answers:

I think the problem will be in your .xls spreadsheet itself. Styles that you chose on your template are incompatible with the current file format (and spreadsheet could be converted to .xlsx, but then you’d probably have to change your implementation to XSSFWorkbook).
You can check if styles are compatible by opening your template, making a minor change and save it – in your case warning should pop up that styles will be converted to the closest equivalents.

Solution: Edit your spreadsheet and pick compatible styles with your .xls format or upgrade to .xlsx format.