【问题标题】:How to check a cell in excel integer or not in apache POI如何检查excel整数中的单元格或不在apache POI中
【发布时间】:2014-08-22 09:51:36
【问题描述】:

我正在使用 apache POI 读取 excel。我要求 excel 中的值只能是整数。我已经这样做了

if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC)

但它适用于所有数值。但我只想检查整数。请帮助。我在这个 XSSFCell 中找不到任何东西来检查整数。

【问题讨论】:

  • 你能不能只获取数字单元格值,这是一个双精度数,并测试该数字是否为整数?
  • 是的,但我想问的是 POI 里面有没有东西..
  • 不,一切都取决于你的规则。例如,如果有人在单元格中写入1.25,然后将其格式化为整数,因此它只显示为1,您是否将其视为整数?您的业​​务规则对于一般情况可能过于具体,因此您只需要编写 3 行代码!

标签: java excel apache apache-poi


【解决方案1】:

你应该尝试添加另一个条件

if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC)
{
    if(cell.getNumberCellValue instanceof Integer)
       do something

}

【讨论】:

  • 那行不通 - 根据Javadocs,该值始终以双精度形式返回
【解决方案2】:

您可以获取单元格值并检查其值是否在四舍五入时发生变化。它不使用 POI 库,但可以完成工作:

Double numericCellValue = cell.getNumericCellValue();
if(Math.floor(numericCellValue) == numericCellValue){
    // It's an integer;
}

【讨论】:

    【解决方案3】:
        Cell value=cols.next();
        if(value.getCellType()==CellType.NUMERIC)
        {
                            
           System.out.println((int)value.getNumericCellValue());
                        
        }
    It will check is cellType is numeric which includes double also. But once confirmed, it will type cast it into numeric value.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多