From 0f17acf12742bf4fb45a8b80d3ba3e17c46ce8c9 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Fri, 12 Jun 2026 16:51:57 +0900 Subject: [PATCH] =?UTF-8?q?260612=5F03=5FENH:=20=EC=97=91=EC=85=80=20?= =?UTF-8?q?=EA=B0=92=20=EC=9D=BD=EC=96=B4=EC=98=A4=EB=8A=94=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=A0=95(=EB=82=A0=EC=A7=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../kri/common/feature/excel/ExcelMapper.java | 11 ++-- .../kri/common/feature/excel/ExcelUtil.java | 63 +++++++------------ 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/build.gradle b/build.gradle index a8ced41..c3ba41b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } // 버전 변경은 여기서 -def commonVer = "beta.1.1.26" +def commonVer = "beta.1.1.27" group = 'com.kri.common' version = commonVer diff --git a/src/main/java/com/kri/common/feature/excel/ExcelMapper.java b/src/main/java/com/kri/common/feature/excel/ExcelMapper.java index 22bfe23..147d4a0 100644 --- a/src/main/java/com/kri/common/feature/excel/ExcelMapper.java +++ b/src/main/java/com/kri/common/feature/excel/ExcelMapper.java @@ -20,6 +20,7 @@ import java.util.Set; import java.util.function.UnaryOperator; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Row; @@ -134,6 +135,7 @@ public final class ExcelMapper { private static List readFromSheet(Sheet sheet, Class clazz, int headerRowIndex) { validateHeaderRowIndex(headerRowIndex); FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator(); + DataFormatter formatter = new DataFormatter(); int headerRowNum = toPoiRowIndex(headerRowIndex); Row headerRow = sheet.getRow(headerRowNum); @@ -146,7 +148,7 @@ public final class ExcelMapper { for (int c = 0; c < lastCellNum; c++) { Cell cell = headerRow.getCell(c); if (cell != null) { - String headerName = ExcelUtil.getStringCell(cell, evaluator); + String headerName = ExcelUtil.getStringCell(cell, evaluator, formatter); if (headerName != null) { headerIndexMap.put(headerName, c); } @@ -173,7 +175,7 @@ public final class ExcelMapper { } Cell cell = row.getCell(colIdx); - String cellValue = ExcelUtil.getStringCell(cell, evaluator); + String cellValue = ExcelUtil.getStringCell(cell, evaluator, formatter); if (cellValue != null) { setFieldValue(instance, meta.field(), cellValue); } @@ -219,6 +221,7 @@ public final class ExcelMapper { private static List> readAsMapListFromSheet(Sheet sheet, int headerRowIndex) { validateHeaderRowIndex(headerRowIndex); FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator(); + DataFormatter formatter = new DataFormatter(); int headerRowNum = toPoiRowIndex(headerRowIndex); Row headerRow = sheet.getRow(headerRowNum); @@ -231,7 +234,7 @@ public final class ExcelMapper { for (int c = 0; c < lastCol; c++) { Cell cell = headerRow.getCell(c); - String headerName = ExcelUtil.getStringCell(cell, evaluator); + String headerName = ExcelUtil.getStringCell(cell, evaluator, formatter); if (headerName == null || headerName.isBlank()) { headerName = "COL_" + c; } @@ -252,7 +255,7 @@ public final class ExcelMapper { for (int c = 0; c < lastCol; c++) { Cell cell = row.getCell(c); - String v = ExcelUtil.getStringCell(cell, evaluator); + String v = ExcelUtil.getStringCell(cell, evaluator, formatter); if (v != null && !v.isBlank()) { allBlank = false; } diff --git a/src/main/java/com/kri/common/feature/excel/ExcelUtil.java b/src/main/java/com/kri/common/feature/excel/ExcelUtil.java index 43ae2ae..301a02b 100644 --- a/src/main/java/com/kri/common/feature/excel/ExcelUtil.java +++ b/src/main/java/com/kri/common/feature/excel/ExcelUtil.java @@ -1,8 +1,7 @@ package com.kri.common.feature.excel; import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellValue; -import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.FormulaEvaluator; public final class ExcelUtil { @@ -11,59 +10,45 @@ public final class ExcelUtil { } public static String getStringCell(Cell cell) { - return getStringCell(cell, null); + return getStringCell(cell, null, new DataFormatter()); } public static String getStringCell(Cell cell, FormulaEvaluator evaluator) { + return getStringCell(cell, evaluator, new DataFormatter()); + } + + public static String getStringCell(Cell cell, FormulaEvaluator evaluator, DataFormatter formatter) { if (cell == null) { return null; } - return switch (cell.getCellType()) { - case STRING -> cell.getStringCellValue(); - case NUMERIC -> getStringNumericCell(cell, cell.getNumericCellValue()); - case BOOLEAN -> String.valueOf(cell.getBooleanCellValue()); - case FORMULA -> getStringFormulaCell(cell, evaluator); - case BLANK -> ""; - default -> null; - }; + DataFormatter cellFormatter = formatter != null ? formatter : new DataFormatter(); + if (evaluator == null) { + return cellFormatter.formatCellValue(cell); + } + + try { + return cellFormatter.formatCellValue(cell, evaluator); + } catch (RuntimeException ignored) { + return formatCachedFormulaCell(cell, cellFormatter); + } } - private static String getStringFormulaCell(Cell cell, FormulaEvaluator evaluator) { - if (evaluator != null) { - try { - CellValue evaluated = evaluator.evaluate(cell); - if (evaluated != null) { - return switch (evaluated.getCellType()) { - case STRING -> evaluated.getStringValue(); - case NUMERIC -> getStringNumericCell(cell, evaluated.getNumberValue()); - case BOOLEAN -> String.valueOf(evaluated.getBooleanValue()); - case BLANK -> ""; - default -> null; - }; - } - } catch (RuntimeException ignored) { - // Fall back to the cached value stored in the workbook. - } + private static String formatCachedFormulaCell(Cell cell, DataFormatter formatter) { + if (cell.getCellType() != org.apache.poi.ss.usermodel.CellType.FORMULA) { + return formatter.formatCellValue(cell); } return switch (cell.getCachedFormulaResultType()) { case STRING -> cell.getStringCellValue(); - case NUMERIC -> getStringNumericCell(cell, cell.getNumericCellValue()); + case NUMERIC -> formatter.formatRawCellContents( + cell.getNumericCellValue(), + cell.getCellStyle().getDataFormat(), + cell.getCellStyle().getDataFormatString() + ); case BOOLEAN -> String.valueOf(cell.getBooleanCellValue()); case BLANK -> ""; default -> null; }; } - - private static String getStringNumericCell(Cell cell, double value) { - if (DateUtil.isCellDateFormatted(cell)) { - return DateUtil.getJavaDate(value).toString(); - } - - if (value == Math.rint(value)) { - return String.valueOf((long) value); - } - return String.valueOf(value); - } }