Always show two decimal points in excel cells using Apache poi

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

For example,

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

productCe         


        
相关标签:
2条回答
  • 2021-01-04 05:25

    This one worked for me:

    1. Set Cell Style

      private CellStyle formatDecimalStyle(Workbook workbook, CreationHelper createHelper) {  
          CellStyle style = workbook.createCellStyle();
          style.setDataFormat(createHelper.createDataFormat().getFormat("0.00"));
          return style;   
      }
      
    2. Apply Style on Cell

      CellStyle style = formatDecimalStyle(workbook, createHelper);
      Cell creditAmountCell = row.createCell(3);
      creditAmountCell.setCellValue(amount);
      creditAmountCell.setCellStyle(style);
      
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题