[英]How to check a cell in excel integer or not in apache POI
我正在使用 apache POI 讀取 excel。我要求 excel 中的值只能是整數。 我已經做到了
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;
}
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.