Apache POI格式化Excel文件中的双数字

时间:2022-02-08 16:01:49

I am using apache-poi for creating some reports. I have an issue with the decimal separator. Right now I have the following displayed in the excel file:

我正在使用apache-poi创建一些报告。我有小数分隔符的问题。现在我在excel文件中显示以下内容:

For 111.2343 -> 111.23

对于111.2343 - > 111.23

For 111.23 -> 111.23

对于111.23 - > 111.23

For 111.2 -> 111.2

对于111.2 - > 111.2

For 111 -> 111. (see the dot at the end)

对于111 - > 111.(见最后的点)

The problem is with 111 number. I don't want to see the trailing dot (or comma, depending on language).

问题是111号码。我不想看到尾随点(或逗号,取决于语言)。

Here is my current code to format the cells. Can this be achieved using apache-poi?

这是我当前格式化单元格的代码。这可以用apache-poi来实现吗?

Thank you,

Iulian

PS: Is there a way to use java.text.Format in poi? I see this has DecimalFormat setDecimalSeparatorAlwaysShown method which does what I want.

PS:有没有办法在poi中使用java.text.Format?我看到这有DecimalFormat setDecimalSeparatorAlwaysShown方法,它做我想要的。

private void createColumnStyle(XSSFSheet sheet, int maxRows,int col)
{
    XSSFWorkbook wb = sheet.getWorkbook();

    XSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short)10);
    font.setFontName("Calibri");

    XSSFCellStyle colStyle = wb.createCellStyle();  
    colStyle.setFont(font);                
    colStyle.setAlignment(HorizontalAlignment.RIGHT);

    colStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    colStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
    colStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
    colStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);

    CreationHelper createHelper = wb.getCreationHelper();                   
    colStyle.setDataFormat(createHelper.createDataFormat().getFormat("#,##0.##"));


    for (int i=3; i<maxRows; i++ )
    {
        XSSFCell cell = sheet.getRow(i).createCell(col);
        cell.setCellStyle(colStyle);
    }

}

1 个解决方案

#1


0  

Using DecimalFormat works fine for all the cases:

使用DecimalFormat适用于所有情况:

DecimalFormat dec = new DecimalFormat("#.00");
double cellValue = Double.valueOf(dec.format(111));
XSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellValue(cellValue);

this set the cell value to 111 only.

这将单元格值设置为111。

#1


0  

Using DecimalFormat works fine for all the cases:

使用DecimalFormat适用于所有情况:

DecimalFormat dec = new DecimalFormat("#.00");
double cellValue = Double.valueOf(dec.format(111));
XSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellValue(cellValue);

this set the cell value to 111 only.

这将单元格值设置为111。