Always show two decimal points in excel cells using Apache poi

前端 未结 2 1319
轮回少年
轮回少年 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);
      

提交回复
热议问题