简体   繁体   中英

How to check a cell in excel integer or not in apache POI

I am using apache POI to read excel.i have a requirement that the the value in the excel should only be integer. I have done this

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

but it is applicable for all numeric values. But i want to check only for integer.Plese help.I dont find any thing in this XSSFCell to check integer.

you should try and add another condition

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

}

You can get the cell value and check if it's value changes when you round it. It doesn't use the POI library, but get's the job done:

Double numericCellValue = cell.getNumericCellValue();
if(Math.floor(numericCellValue) == numericCellValue){
    // It's an integer;
}
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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