260612_04_ENH: 엑셀 값 읽어오는 부분 수정(소수점 숫자)
This commit is contained in:
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 버전 변경은 여기서
|
// 버전 변경은 여기서
|
||||||
def commonVer = "beta.1.1.27"
|
def commonVer = "beta.1.1.28"
|
||||||
|
|
||||||
group = 'com.kri.common'
|
group = 'com.kri.common'
|
||||||
version = commonVer
|
version = commonVer
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package com.kri.common.feature.excel;
|
package com.kri.common.feature.excel;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
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.DataFormatter;
|
||||||
|
import org.apache.poi.ss.usermodel.DateUtil;
|
||||||
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
|
import org.apache.poi.ss.util.NumberToTextConverter;
|
||||||
|
|
||||||
public final class ExcelUtil {
|
public final class ExcelUtil {
|
||||||
|
|
||||||
@@ -23,32 +27,62 @@ public final class ExcelUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DataFormatter cellFormatter = formatter != null ? formatter : new DataFormatter();
|
DataFormatter cellFormatter = formatter != null ? formatter : new DataFormatter();
|
||||||
if (evaluator == null) {
|
return switch (cell.getCellType()) {
|
||||||
return cellFormatter.formatCellValue(cell);
|
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 formatCachedFormulaCell(cell, formatter);
|
||||||
return cellFormatter.formatCellValue(cell, evaluator);
|
|
||||||
} catch (RuntimeException ignored) {
|
|
||||||
return formatCachedFormulaCell(cell, cellFormatter);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String formatCachedFormulaCell(Cell cell, DataFormatter 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 formatter.formatCellValue(cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
return switch (cell.getCachedFormulaResultType()) {
|
return switch (cell.getCachedFormulaResultType()) {
|
||||||
case STRING -> cell.getStringCellValue();
|
case STRING -> cell.getStringCellValue();
|
||||||
case NUMERIC -> formatter.formatRawCellContents(
|
case NUMERIC -> formatNumericCell(cell, cell.getNumericCellValue(), formatter);
|
||||||
cell.getNumericCellValue(),
|
|
||||||
cell.getCellStyle().getDataFormat(),
|
|
||||||
cell.getCellStyle().getDataFormatString()
|
|
||||||
);
|
|
||||||
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
|
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
|
||||||
case BLANK -> "";
|
case BLANK -> "";
|
||||||
default -> null;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user