繁体   English   中英

Apache POI在Excel文件中格式化双精度数字

[英]Apache POI formatting double numbers in Excel files

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

对于111.2343-> 111.23

对于111.23-> 111.23

对于111.2-> 111.2

对于111->111。 (请参阅最后的点)

问题出在111号。 我不想看到结尾的点(或逗号,取决于语言)。

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

谢谢,

尤利安

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);
    }

}

在所有情况下,使用DecimalFormat可以正常工作:

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

将此单元格值仅设置为111

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM