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); + } }