package org.jeecg.modules.tms.utils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import java.text.SimpleDateFormat; public class ExcelUtils { // 辅助方法:根据单元格类型安全地获取字符串值 public static String getCellValueAsString(Cell cell) { if (cell == null) { return null; } switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { // 如果是日期格式,返回日期字符串 return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()); } else { // 否则返回数值的字符串形式 return String.valueOf(cell.getNumericCellValue()); } case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case FORMULA: try { return String.valueOf(cell.getNumericCellValue()); // 尝试解析公式结果 } catch (Exception e) { return String.valueOf(cell.getStringCellValue()); // 如果公式结果为字符串 } default: return ""; } } // 判断某一行是否全为空(有效数据行判断) public static boolean isRowEmpty(Row row) { if (row == null) return true; for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null && cell.getCellType() != CellType.BLANK) { return false; } } return true; } }