From 5c5355ea1533ba66bc2e24ab060ef358ccb93785 Mon Sep 17 00:00:00 2001 From: "han\\hanst" Date: Wed, 24 Dec 2025 10:00:31 +0800 Subject: [PATCH] =?UTF-8?q?Java=E7=BC=96=E8=AF=91=E5=99=A8=E5=9C=A8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=20switch=20=E8=AF=AD=E5=8F=A5=E6=97=B6=EF=BC=8C=E4=BC=9A?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E5=90=88=E6=88=90=E5=86=85?= =?UTF-8?q?=E9=83=A8=E7=B1=BB=EF=BC=88synthetic=20class=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E4=B8=BA=20$1,=20$2,=20$3=20=E7=AD=89?= =?UTF-8?q?=E3=80=82=E5=BD=93=E8=BF=99=E4=BA=9B=E5=86=85=E9=83=A8=E7=B1=BB?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=A2=E5=A4=B1=E6=88=96=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E4=B8=8D=E5=AE=8C=E6=95=B4=E6=97=B6=EF=BC=8C=E5=B0=B1=E4=BC=9A?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=20NoClassDefFoundError=E3=80=82=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E5=8E=9F=E7=90=86=EF=BC=9A=E4=BD=BF=E7=94=A8=20if-els?= =?UTF-8?q?e=20=E8=AF=AD=E5=8F=A5=E7=9B=B4=E6=8E=A5=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E5=80=BC=EF=BC=8C=E9=81=BF=E5=85=8D=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E7=94=9F=E6=88=90=E9=A2=9D=E5=A4=96=E7=9A=84?= =?UTF-8?q?=E5=86=85=E9=83=A8=E7=B1=BB=EF=BC=8C=E4=BB=8E=E6=A0=B9=E6=9C=AC?= =?UTF-8?q?=E4=B8=8A=E6=B6=88=E9=99=A4=E4=BA=86=E8=BF=99=E4=B8=AA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CoDelExcelServiceImpl.java | 167 +++++++++--------- 1 file changed, 84 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelServiceImpl.java b/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelServiceImpl.java index 71452db6..3e4b059c 100644 --- a/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelServiceImpl.java +++ b/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelServiceImpl.java @@ -761,22 +761,19 @@ public class CoDelExcelServiceImpl implements CoDelExcelService { } // 处理公式类型 - 获取缓存结果 else if (cellType == CellType.FORMULA) { - switch (cell.getCachedFormulaResultType()) { - case NUMERIC: - // 公式结果是数值类型且是日期格式 - if (DateUtil.isCellDateFormatted(cell)) { - return cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); - } - break; - case STRING: - // 公式结果是字符串类型 - String formulaVal = cell.getStringCellValue().trim(); - if (!formulaVal.isEmpty()) { - return LocalDate.parse(formulaVal, DateTimeFormatter.ofPattern("yyyy-MM-dd")); - } - break; - default: - break; + CellType cachedType = cell.getCachedFormulaResultType(); + + if (cachedType == CellType.NUMERIC) { + // 公式结果是数值类型且是日期格式 + if (DateUtil.isCellDateFormatted(cell)) { + return cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + } + } else if (cachedType == CellType.STRING) { + // 公式结果是字符串类型 + String formulaVal = cell.getStringCellValue().trim(); + if (!formulaVal.isEmpty()) { + return LocalDate.parse(formulaVal, DateTimeFormatter.ofPattern("yyyy-MM-dd")); + } } } } catch (Exception e) { @@ -811,32 +808,33 @@ public class CoDelExcelServiceImpl implements CoDelExcelService { // 处理公式类型 - 获取缓存结果 if (cellType == CellType.FORMULA) { - switch (cell.getCachedFormulaResultType()) { - case NUMERIC: - // 公式结果是数值类型 - 直接获取数值并转换为字符串 - // 检查是否是日期格式 - if (DateUtil.isCellDateFormatted(cell)) { - DataFormatter formatter = new DataFormatter(); - return formatter.formatCellValue(cell); + CellType cachedType = cell.getCachedFormulaResultType(); + + if (cachedType == CellType.NUMERIC) { + // 公式结果是数值类型 - 直接获取数值并转换为字符串 + // 检查是否是日期格式 + if (DateUtil.isCellDateFormatted(cell)) { + DataFormatter formatter = new DataFormatter(); + return formatter.formatCellValue(cell); + } else { + // 普通数值,转换为字符串(去除小数点后的零) + double numericValue = cell.getNumericCellValue(); + // 如果是整数,去掉.0 + if (numericValue == Math.floor(numericValue)) { + return String.valueOf((long) numericValue); } else { - // 普通数值,转换为字符串(去除小数点后的零) - double numericValue = cell.getNumericCellValue(); - // 如果是整数,去掉.0 - if (numericValue == Math.floor(numericValue)) { - return String.valueOf((long) numericValue); - } else { - return String.valueOf(numericValue); - } + return String.valueOf(numericValue); } - case STRING: - // 公式结果是字符串类型 - return cell.getStringCellValue(); - case BLANK: - return ""; - case BOOLEAN: - return String.valueOf(cell.getBooleanCellValue()); - default: - return ""; + } + } else if (cachedType == CellType.STRING) { + // 公式结果是字符串类型 + return cell.getStringCellValue(); + } else if (cachedType == CellType.BLANK) { + return ""; + } else if (cachedType == CellType.BOOLEAN) { + return String.valueOf(cell.getBooleanCellValue()); + } else { + return ""; } } @@ -869,17 +867,18 @@ public class CoDelExcelServiceImpl implements CoDelExcelService { // 处理公式类型 - 获取缓存结果 if (cellType == CellType.FORMULA) { - switch (cell.getCachedFormulaResultType()) { - case NUMERIC: - return (int) Math.round(cell.getNumericCellValue()); - case STRING: - try { - return Integer.parseInt(cell.getStringCellValue().trim()); - } catch (NumberFormatException e) { - return -1; - } - default: + CellType cachedType = cell.getCachedFormulaResultType(); + + if (cachedType == CellType.NUMERIC) { + return (int) Math.round(cell.getNumericCellValue()); + } else if (cachedType == CellType.STRING) { + try { + return Integer.parseInt(cell.getStringCellValue().trim()); + } catch (NumberFormatException e) { return -1; + } + } else { + return -1; } } @@ -911,11 +910,32 @@ public class CoDelExcelServiceImpl implements CoDelExcelService { boolean isRequiredColumn = columnName != null && ("Qty (pcs)".equalsIgnoreCase(columnName) || "TP".equalsIgnoreCase(columnName)); - switch (cell.getCellType()) { - case NUMERIC: - BigDecimal value = BigDecimal.valueOf(cell.getNumericCellValue()); - return value.setScale(6, RoundingMode.HALF_UP); // 四舍五入保留四位小数 - case STRING: + CellType cellType = cell.getCellType(); + + if (cellType == CellType.NUMERIC) { + BigDecimal value = BigDecimal.valueOf(cell.getNumericCellValue()); + return value.setScale(6, RoundingMode.HALF_UP); // 四舍五入保留四位小数 + } else if (cellType == CellType.STRING) { + try { + if (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty()) { + return null; + } + BigDecimal stringValue = new BigDecimal(cell.getStringCellValue()); + return stringValue.setScale(6, RoundingMode.HALF_UP); // 四舍五入保留四位小数 + } catch (NumberFormatException e) { + if (isRequiredColumn) { + throw new RuntimeException("[" + columnName + "] 列无效的数值格式: " + cell.getStringCellValue()); + } + return null; + } + } else if (cellType == CellType.FORMULA) { + // 获取缓存结果 + CellType cachedType = cell.getCachedFormulaResultType(); + + if (cachedType == CellType.NUMERIC) { + BigDecimal formulaValue = BigDecimal.valueOf(cell.getNumericCellValue()); + return formulaValue.setScale(6, RoundingMode.HALF_UP); // 四舍五入保留四位小数 + } else if (cachedType == CellType.STRING) { try { if (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty()) { return null; @@ -928,35 +948,16 @@ public class CoDelExcelServiceImpl implements CoDelExcelService { } return null; } - case FORMULA: - // 获取缓存结果 - switch (cell.getCachedFormulaResultType()) { - case NUMERIC: - BigDecimal formulaValue = BigDecimal.valueOf(cell.getNumericCellValue()); - return formulaValue.setScale(6, RoundingMode.HALF_UP); // 四舍五入保留四位小数 - case STRING: - try { - if (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty()) { - return null; - } - BigDecimal stringValue = new BigDecimal(cell.getStringCellValue()); - return stringValue.setScale(6, RoundingMode.HALF_UP); // 四舍五入保留四位小数 - } catch (NumberFormatException e) { - if (isRequiredColumn) { - throw new RuntimeException("[" + columnName + "] 列无效的数值格式: " + cell.getStringCellValue()); - } - return null; - } - default: - return null; - } - default: - // 如果是必填列,抛出异常提示 - if (isRequiredColumn) { - throw new RuntimeException("[" + columnName + "] 列不支持的单元格类型: " + cell.getCellType()); - } - // 其他非必填列返回null + } else { return null; + } + } else { + // 如果是必填列,抛出异常提示 + if (isRequiredColumn) { + throw new RuntimeException("[" + columnName + "] 列不支持的单元格类型: " + cell.getCellType()); + } + // 其他非必填列返回null + return null; } }