Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 始终使用Apache poi在excel单元格中显示两个小数点_Java_Excel_Apache Poi_Excel 2007 - Fatal编程技术网

Java 始终使用Apache poi在excel单元格中显示两个小数点

Java 始终使用Apache poi在excel单元格中显示两个小数点,java,excel,apache-poi,excel-2007,Java,Excel,Apache Poi,Excel 2007,比如说, XSSFCellStyle style=(XSSFCellStyle) workbook.createCellStyle(); style.setDataFormat(workbook.createDataFormat().getFormat("#.##")); productCell.setCellValue(12.4); productCell.setCellType(Cell.CELL_TYPE_NUMERIC); productCell.setCellStyle(style)

比如说,

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

productCell.setCellValue(12.4);
productCell.setCellType(Cell.CELL_TYPE_NUMERIC);
productCell.setCellStyle(style);
这将在指定单元格中显示
12.4
。它应该是
12.40
。值
12
显示为
12.
,这是非常不必要的

如果该值为
0
,则会显示一个点
。它应始终显示两个十进制数字-
0.00
,在这种情况下,无论存储在单元格中的值是多少

如何强制excel在数字单元格中始终显示两位十进制数字


我使用以下样式之一来显示数字单元格

XSSFColor commonColor = new XSSFColor(new java.awt.Color(240, 240, 240));
XSSFColor cellBorderColour = new XSSFColor(new java.awt.Color(0, 76, 153));

Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setColor(IndexedColors.DARK_BLUE.index);

XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
style.setFillForegroundColor(commonColor);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font);

style.setBorderLeft(BorderStyle.HAIR);
style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, cellBorderColour);
style.setBorderTop(BorderStyle.HAIR);
style.setBorderColor(XSSFCellBorder.BorderSide.TOP, cellBorderColour);
style.setBorderRight(BorderStyle.HAIR);
style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, cellBorderColour);
style.setBorderBottom(BorderStyle.DOUBLE);
style.setBottomBorderColor(cellBorderColour);

我应用了一些excel公式来对数字单元格执行一些计算,这些计算是在对数字单元格应用数字格式(向上取整)后执行的。

在excel格式中,
表示“仅在需要时在此处放置数字”,而
0表示“始终在此处放置数字,即使它是不必要的0”。您可以精确指定。如果希望显示
0
数字,则需要使用
0
s作为格式化数字。请尝试

XSSFColor commonColor = new XSSFColor(new java.awt.Color(240, 240, 240));
XSSFColor cellBorderColour = new XSSFColor(new java.awt.Color(0, 76, 153));

Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setColor(IndexedColors.DARK_BLUE.index);

XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
style.setFillForegroundColor(commonColor);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font);

style.setBorderLeft(BorderStyle.HAIR);
style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, cellBorderColour);
style.setBorderTop(BorderStyle.HAIR);
style.setBorderColor(XSSFCellBorder.BorderSide.TOP, cellBorderColour);
style.setBorderRight(BorderStyle.HAIR);
style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, cellBorderColour);
style.setBorderBottom(BorderStyle.DOUBLE);
style.setBottomBorderColor(cellBorderColour);
style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));

这将使值
0
显示为
0.00
12.4
显示为
12.40
,这一个对我有用:

  • 设置单元格样式

    private CellStyle formatDecimalStyle(Workbook workbook, CreationHelper createHelper) {  
        CellStyle style = workbook.createCellStyle();
        style.setDataFormat(createHelper.createDataFormat().getFormat("0.00"));
        return style;   
    }
    
  • 在单元格上应用样式

    CellStyle style = formatDecimalStyle(workbook, createHelper);
    Cell creditAmountCell = row.createCell(3);
    creditAmountCell.setCellValue(amount);
    creditAmountCell.setCellStyle(style);
    

  • 您尝试过这个吗?是的,它的行为与问题中提到的完全相同。如果您在Excel中格式化单元格,您需要使用什么单元格样式才能使其看起来像您希望的那样?编辑了问题。@Gagravarr请告诉我们如何获取3位小数。
    style.setDataFormat(workbook.createDataFormat().getFormat(“0.000”))
    但是,如果使用0.######,整数的格式是在末尾加一个点。例如,33显示为33。如果使用#.###,则根本不显示0。