Java 如何在excel integer中检查单元格,或不在apache POI中检查单元格
我使用ApachePOI来读取excel。我有一个要求,excel中的值只能是整数。我已经这样做了Java 如何在excel integer中检查单元格,或不在apache POI中检查单元格,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我使用ApachePOI来读取excel。我有一个要求,excel中的值只能是整数。我已经这样做了 if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC) 但它适用于所有数值。但是我只想检查整数。请帮助。我在这个XSSFCell中找不到任何东西来检查整数。您应该尝试添加另一个条件 if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC) { if(cell.getNumberCellValue i
if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC)
但它适用于所有数值。但是我只想检查整数。请帮助。我在这个XSSFCell中找不到任何东西来检查整数。您应该尝试添加另一个条件
if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC)
{
if(cell.getNumberCellValue instanceof Integer)
do something
}
您可以获取单元格值,并在对其进行四舍五入时检查其值是否更改。它不使用POI库,但完成了任务:
Double numericCellValue = cell.getNumericCellValue();
if(Math.floor(numericCellValue) == numericCellValue){
// It's an integer;
}
你能不能先取一个双精度的数字单元格值,然后测试这个数字是否是一个整数?是的,这是一种方法,但我想问的是POI里面是否有什么东西……不,这取决于你的规则。例如,如果有人在单元格中写入
1.25
,然后将其格式化为整数,使其显示为1
,您是否将其计为整数?对于一般情况,您的业务规则可能过于具体,因此您只需要编写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.