使用getCellTypeEnum()方法判断单元格中数据类型,并根据对应类型使用相应方法获取值。
代码示例:
Cell cell = row.getCell(col); if (cell.getCellTypeEnum() == CellType.STRING) { String cellValue = cell.getStringCellValue(); } else if (cell.getCellTypeEnum() == CellType.NUMERIC) { double cellValue = cell.getNumericCellValue(); } else if (cell.getCellTypeEnum() == CellType.BLANK) { String cellValue = ""; } else if (cell.getCellTypeEnum() == CellType.BOOLEAN) { boolean cellValue = cell.getBooleanCellValue(); } else if (cell.getCellTypeEnum() == CellType.ERROR) { String cellValue = "ERROR"; } else if (cell.getCellTypeEnum() == CellType.FORMULA) { String cellValue = cell.getCellFormula(); }