java如何判断数据类型

2024-11-15 10:41:04
推荐回答(3个)
回答1:

此答案为转载:
给你一个封装好的方法,只要把excel中的cell放入就会返回对应的悄历值,里面樱运盯有类脊和型检测

public static String getExcelCellValue(HSSFCell cell) {
String ret = "";
// if (HSSFDateUtil.isCellDateFormatted(cell)) {
// Date date = cell.getDateCellValue();
// ret = "" + date.getTime();
// } else
try {
if (cell == null) {
ret = "";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
ret = cell.getStringCellValue().trim();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
ret = "" + cell.getNumericCellValue();
String temp = ret.substring(ret.indexOf(".") + 1, ret.length());
try {
if (Integer.parseInt(temp) == 0) {
ret = ret.substring(0, ret.indexOf("."));
}
} catch (Exception ex) {
}
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
ret = cell.getCellFormula();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
ret = "" + cell.getErrorCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
ret = "" + cell.getBooleanCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
ret = "";
}
} catch (Exception ex) {
ex.printStackTrace();
ret = "";
}

return ret;
}

回答2:

通过正则表达式方式。

回答3: