EXCEl中关于Cell中的各种值的类型判断

tech2025-12-23  9

private static String getCellValue(XSSFCell cell) { // DecimalFormat df = new DecimalFormat("#.#"); String cellValue = null; if (cell == null) return null; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC : if (HSSFDateUtil.isCellDateFormatted(cell)) { // 需要对日期这一列进行设置样式,否则无法识别是日期类型还是数值类型 // 默认不支持中文日期类型,需要设置成纯英语日期类型,不要包含年月日等汉字 // 最好是使用这种格式 2019/10/10 0:00 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); cellValue = sdf.format(HSSFDateUtil.getJavaDate(cell .getNumericCellValue())); break; } // cellValue = df.format(cell.getNumericCellValue()); String v=String.format("%.4f",cell.getNumericCellValue()); //默认的整数后边是一个小数点,和一个零 cellValue=(v).replaceAll("\\.0*$","");//整数作为浮点数格式化以后,删除结尾的点零 break; case HSSFCell.CELL_TYPE_STRING : cellValue = String.valueOf(cell.getStringCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA ://XSSFCell.getCTCell() cellValue = cell.getCTCell().getV();//.getCachedFormulaResultType();//String.valueOf(cell..getCellFormula()); break; case HSSFCell.CELL_TYPE_BLANK : cellValue = null; break; case HSSFCell.CELL_TYPE_BOOLEAN : cellValue = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR : cellValue = String.valueOf(cell.getErrorCellValue()); break; } if (cellValue != null && cellValue.trim().length() <= 0) { cellValue = null; } return cellValue; } switch (cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC: //数字 cellValue = stringDateProcess(cell); break; case Cell.CELL_TYPE_STRING: //字符串 cellValue = String.valueOf(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: //Boolean cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: //公式 cellValue = String.valueOf(cell.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: //空值 cellValue = ""; break; case Cell.CELL_TYPE_ERROR: //故障 cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; }
最新回复(0)