Java – Negative Cell Value Styles in Apache POI

apacheapache-poiexceljava

Using Apache POI to generate a document and i'm having a small problem with cell styles, currentlly i'm using:

CellStyle currencyCellStyle=workbook.createCellStyle();
currencyCellStyle.setDataFormat(format.getFormat("$#,##0.00"));

Which works totally fine for positive numbers, however I would like to assign a different style to negative cells automatically.

Question is is there any to set this up without having to check the individual cell values and assign a separate style to them?

Or alternatively is there any way to tell Apache POI to use the built in excel currency format with one of its negative options?

Best Answer

Found it, thanks me :D

CellStyle currencyCellStyle=workbook.createCellStyle();
currencyCellStyle.setDataFormat(format.getFormat("$#,##0.00;[Red]($#,##0.00)"));