For example,
XSSFCellStyle style=(XSSFCellStyle) workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat(\"#.##\"));
productCe
This one worked for me:
Set Cell Style
private CellStyle formatDecimalStyle(Workbook workbook, CreationHelper createHelper) {
CellStyle style = workbook.createCellStyle();
style.setDataFormat(createHelper.createDataFormat().getFormat("0.00"));
return style;
}
Apply Style on Cell
CellStyle style = formatDecimalStyle(workbook, createHelper);
Cell creditAmountCell = row.createCell(3);
creditAmountCell.setCellValue(amount);
creditAmountCell.setCellStyle(style);
In Excel formatting, a #
means "place a digit here only if needed", but a 0
means "always place a digit here, even if it's an unnecessary 0". You can specify the data format in Apache POI exactly as you would in Excel itself. If you want the 0
digits to show up, then you need to use 0
s as formatting digits. Try
style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
This will make the value 0
show up as 0.00
and 12.4
as 12.40
.