POI解析excel,针对日期格式06-六月-2020的处理方式 方法二

it2025-08-15  13

这里直接根据数据类型来判断的,excel输入日期为2020/06/06, 根据cell.getCellType()方法判断为数值型类型, 然后使用cell.getNumericCellValue()方法,获取dauble值, 然后跟DateUtil.getJavaDate(d)方法获取到日期 private static SimpleDateFormat sdfYmd = new SimpleDateFormat(“yyyy-MM-dd”); private String getCellString(Cell cell) throws Exception { String result = “”; if (cell != null) { int cellType = cell.getCellType(); switch (cellType) { //CELL_TYPE_STRING  字符串型  1 case Cell.CELL_TYPE_STRING: result = cell.getRichStringCellValue().getString(); break; //CELL_TYPE_NUMERIC  数值型  0 case Cell.CELL_TYPE_NUMERIC: double d = cell.getNumericCellValue(); Date date = DateUtil.getJavaDate(d); result = sdfYmd.format(date); System.out.println(result); break; //CELL_TYPE_FORMULA  公式型 2 case Cell.CELL_TYPE_FORMULA: result = String.valueOf(cell.getNumericCellValue()); break; //CELL_TYPE_BOOLEAN  布尔型  4 case Cell.CELL_TYPE_BOOLEAN: result = String.valueOf(cell.getBooleanCellValue()); break; //CELL_TYPE_ERROR   错误   5 case Cell.CELL_TYPE_ERROR: result = null; break; //CELL_TYPE_BLANK   空值   3 case Cell.CELL_TYPE_BLANK: result = “”; break; default: result = “”; break; } } return result; }

最新回复(0)