java - 对 apache poi ss 工作簿中的数字进行四舍五入

标签 java excel apache-poi

我试图以精度值 2 舍入小数,但它不起作用。让我知道下面代码中的问题:

CellStyle cs = wb.createCellStyle();
DataFormat df = wb.createDataFormat();
cs.setDataFormat(df.getFormat("0.0"));


for (int i=14;i<rows;i++)
{
    Cell  y=   wb.getSheetAt(0).getRow(i).getCell((short) colValue);

    if (y.getCellType() == Cell.CELL_TYPE_STRING) {

    } else if (y.getCellType() == Cell.CELL_TYPE_NUMERIC) {

        float d=(float) y.getNumericCellValue();
        y.setCellStyle(cs);
    }
}  

最佳答案

像这样使用:

CellStyle cs = wb.createCellStyle();
cs.setDataFormat(wb
  .getCreationHelper()
  .createDataFormat()
  .getFormat("##.##"));
y.setCellStyle(cs);

关于java - 对 apache poi ss 工作簿中的数字进行四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797406/

相关文章:

java - 在 android 中使用 GCM 服务

excel - 如何在excel中快速将UTC时间转换为本地时间?

java - 如何使用 HSSFFont 以十进制数设置字体

java - 在 Java 中使用流进行序列化

java - 我们如何将 log4j 消息存储到 java util 列表

java - MongoDB实现保存历史对象变化的数据库结构

vba - 如何使用 VBA 关闭 Excel 中的网格线,而不使用 ActiveWindow

Java 到 Excel - 动态更新

java - 当我使用 poi 和 HSSFListener 处理记录时,如何获取 excel(xls) 单元格注释?

java - Apache POI : xlsx not opening the file with MS Office excel