Always show two decimal points in excel cells using Apache poi

前端 未结 2 1323
轮回少年
轮回少年 2021-01-04 05:09

For example,

XSSFCellStyle style=(XSSFCellStyle) workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat(\"#.##\"));

productCe         


        
2条回答
  •  青春惊慌失措
    2021-01-04 05:38

    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 0s 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.

提交回复
热议问题