260612_04_ENH: 엑셀 값 읽어오는 부분 수정(소수점 숫자)

This commit is contained in:
2026-06-12 16:54:51 +09:00
parent 0f17acf127
commit 42b2c0442f
2 changed files with 48 additions and 14 deletions

View File

@@ -4,7 +4,7 @@ plugins {
}
// 버전 변경은 여기서
def commonVer = "beta.1.1.27"
def commonVer = "beta.1.1.28"
group = 'com.kri.common'
version = commonVer

View File

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