From 42b2c0442f790a78ff097042d511f64b5d7d44b4 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Fri, 12 Jun 2026 16:54:51 +0900 Subject: [PATCH] =?UTF-8?q?260612=5F04=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(=EC=86=8C=EC=88=98=EC=A0=90=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../kri/common/feature/excel/ExcelUtil.java | 60 +++++++++++++++---- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index c3ba41b..bb959b1 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } // 버전 변경은 여기서 -def commonVer = "beta.1.1.27" +def commonVer = "beta.1.1.28" group = 'com.kri.common' version = commonVer 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 301a02b..a7f47ff 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,12 @@ package com.kri.common.feature.excel; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.util.NumberToTextConverter; public final class ExcelUtil { @@ -23,32 +27,62 @@ public final class ExcelUtil { } DataFormatter cellFormatter = formatter != null ? formatter : new DataFormatter(); - if (evaluator == null) { - return cellFormatter.formatCellValue(cell); + return switch (cell.getCellType()) { + case NUMERIC -> formatNumericCell(cell, cell.getNumericCellValue(), cellFormatter); + case FORMULA -> formatFormulaCell(cell, evaluator, cellFormatter); + case STRING, BOOLEAN, BLANK -> cellFormatter.formatCellValue(cell); + default -> null; + }; + } + + private static String formatFormulaCell( + Cell cell, + FormulaEvaluator evaluator, + DataFormatter formatter + ) { + try { + if (evaluator != null) { + CellValue evaluated = evaluator.evaluate(cell); + if (evaluated != null) { + return switch (evaluated.getCellType()) { + case STRING -> evaluated.getStringValue(); + case NUMERIC -> formatNumericCell(cell, evaluated.getNumberValue(), formatter); + case BOOLEAN -> String.valueOf(evaluated.getBooleanValue()); + case BLANK -> ""; + default -> null; + }; + } + } + } catch (RuntimeException ignored) { + // Fall back to the cached value stored in the workbook. } - try { - return cellFormatter.formatCellValue(cell, evaluator); - } catch (RuntimeException ignored) { - return formatCachedFormulaCell(cell, cellFormatter); - } + return formatCachedFormulaCell(cell, formatter); } private static String formatCachedFormulaCell(Cell cell, DataFormatter formatter) { - if (cell.getCellType() != org.apache.poi.ss.usermodel.CellType.FORMULA) { + if (cell.getCellType() != CellType.FORMULA) { return formatter.formatCellValue(cell); } return switch (cell.getCachedFormulaResultType()) { case STRING -> cell.getStringCellValue(); - case NUMERIC -> formatter.formatRawCellContents( - cell.getNumericCellValue(), - cell.getCellStyle().getDataFormat(), - cell.getCellStyle().getDataFormatString() - ); + case NUMERIC -> formatNumericCell(cell, cell.getNumericCellValue(), formatter); case BOOLEAN -> String.valueOf(cell.getBooleanCellValue()); case BLANK -> ""; default -> null; }; } + + private static String formatNumericCell(Cell cell, double value, DataFormatter formatter) { + if (DateUtil.isCellDateFormatted(cell)) { + return formatter.formatRawCellContents( + value, + cell.getCellStyle().getDataFormat(), + cell.getCellStyle().getDataFormatString() + ); + } + + return NumberToTextConverter.toText(value); + } }