260612_02_ENH: 엑셀 값 읽어오는 부분 수정
This commit is contained in:
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 버전 변경은 여기서
|
// 버전 변경은 여기서
|
||||||
def commonVer = "beta.1.1.25"
|
def commonVer = "beta.1.1.26"
|
||||||
|
|
||||||
group = 'com.kri.common'
|
group = 'com.kri.common'
|
||||||
version = commonVer
|
version = commonVer
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import java.util.Set;
|
|||||||
import java.util.function.UnaryOperator;
|
import java.util.function.UnaryOperator;
|
||||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
import org.apache.poi.ss.usermodel.RichTextString;
|
import org.apache.poi.ss.usermodel.RichTextString;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
@@ -132,6 +133,7 @@ public final class ExcelMapper {
|
|||||||
|
|
||||||
private static <T> List<T> readFromSheet(Sheet sheet, Class<T> clazz, int headerRowIndex) {
|
private static <T> List<T> readFromSheet(Sheet sheet, Class<T> clazz, int headerRowIndex) {
|
||||||
validateHeaderRowIndex(headerRowIndex);
|
validateHeaderRowIndex(headerRowIndex);
|
||||||
|
FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();
|
||||||
|
|
||||||
int headerRowNum = toPoiRowIndex(headerRowIndex);
|
int headerRowNum = toPoiRowIndex(headerRowIndex);
|
||||||
Row headerRow = sheet.getRow(headerRowNum);
|
Row headerRow = sheet.getRow(headerRowNum);
|
||||||
@@ -144,7 +146,7 @@ public final class ExcelMapper {
|
|||||||
for (int c = 0; c < lastCellNum; c++) {
|
for (int c = 0; c < lastCellNum; c++) {
|
||||||
Cell cell = headerRow.getCell(c);
|
Cell cell = headerRow.getCell(c);
|
||||||
if (cell != null) {
|
if (cell != null) {
|
||||||
String headerName = ExcelUtil.getStringCell(cell);
|
String headerName = ExcelUtil.getStringCell(cell, evaluator);
|
||||||
if (headerName != null) {
|
if (headerName != null) {
|
||||||
headerIndexMap.put(headerName, c);
|
headerIndexMap.put(headerName, c);
|
||||||
}
|
}
|
||||||
@@ -171,7 +173,7 @@ public final class ExcelMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cell cell = row.getCell(colIdx);
|
Cell cell = row.getCell(colIdx);
|
||||||
String cellValue = ExcelUtil.getStringCell(cell);
|
String cellValue = ExcelUtil.getStringCell(cell, evaluator);
|
||||||
if (cellValue != null) {
|
if (cellValue != null) {
|
||||||
setFieldValue(instance, meta.field(), cellValue);
|
setFieldValue(instance, meta.field(), cellValue);
|
||||||
}
|
}
|
||||||
@@ -216,6 +218,7 @@ public final class ExcelMapper {
|
|||||||
|
|
||||||
private static List<Map<String, String>> readAsMapListFromSheet(Sheet sheet, int headerRowIndex) {
|
private static List<Map<String, String>> readAsMapListFromSheet(Sheet sheet, int headerRowIndex) {
|
||||||
validateHeaderRowIndex(headerRowIndex);
|
validateHeaderRowIndex(headerRowIndex);
|
||||||
|
FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();
|
||||||
|
|
||||||
int headerRowNum = toPoiRowIndex(headerRowIndex);
|
int headerRowNum = toPoiRowIndex(headerRowIndex);
|
||||||
Row headerRow = sheet.getRow(headerRowNum);
|
Row headerRow = sheet.getRow(headerRowNum);
|
||||||
@@ -228,7 +231,7 @@ public final class ExcelMapper {
|
|||||||
|
|
||||||
for (int c = 0; c < lastCol; c++) {
|
for (int c = 0; c < lastCol; c++) {
|
||||||
Cell cell = headerRow.getCell(c);
|
Cell cell = headerRow.getCell(c);
|
||||||
String headerName = ExcelUtil.getStringCell(cell);
|
String headerName = ExcelUtil.getStringCell(cell, evaluator);
|
||||||
if (headerName == null || headerName.isBlank()) {
|
if (headerName == null || headerName.isBlank()) {
|
||||||
headerName = "COL_" + c;
|
headerName = "COL_" + c;
|
||||||
}
|
}
|
||||||
@@ -249,7 +252,7 @@ public final class ExcelMapper {
|
|||||||
|
|
||||||
for (int c = 0; c < lastCol; c++) {
|
for (int c = 0; c < lastCol; c++) {
|
||||||
Cell cell = row.getCell(c);
|
Cell cell = row.getCell(c);
|
||||||
String v = ExcelUtil.getStringCell(cell);
|
String v = ExcelUtil.getStringCell(cell, evaluator);
|
||||||
if (v != null && !v.isBlank()) {
|
if (v != null && !v.isBlank()) {
|
||||||
allBlank = false;
|
allBlank = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
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.CellValue;
|
||||||
import org.apache.poi.ss.usermodel.DateUtil;
|
import org.apache.poi.ss.usermodel.DateUtil;
|
||||||
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
|
|
||||||
public final class ExcelUtil {
|
public final class ExcelUtil {
|
||||||
|
|
||||||
@@ -9,27 +11,59 @@ public final class ExcelUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getStringCell(Cell cell) {
|
public static String getStringCell(Cell cell) {
|
||||||
|
return getStringCell(cell, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getStringCell(Cell cell, FormulaEvaluator evaluator) {
|
||||||
if (cell == null) {
|
if (cell == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return switch (cell.getCellType()) {
|
return switch (cell.getCellType()) {
|
||||||
case STRING -> cell.getStringCellValue();
|
case STRING -> cell.getStringCellValue();
|
||||||
case NUMERIC -> {
|
case NUMERIC -> getStringNumericCell(cell, cell.getNumericCellValue());
|
||||||
if (DateUtil.isCellDateFormatted(cell)) {
|
|
||||||
yield cell.getDateCellValue().toString();
|
|
||||||
} else {
|
|
||||||
double d = cell.getNumericCellValue();
|
|
||||||
if (d == Math.rint(d)) {
|
|
||||||
yield String.valueOf((long) d);
|
|
||||||
}
|
|
||||||
yield String.valueOf(d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
|
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
|
||||||
case FORMULA -> cell.getCellFormula();
|
case FORMULA -> getStringFormulaCell(cell, evaluator);
|
||||||
case BLANK -> null;
|
case BLANK -> "";
|
||||||
default -> null; // ERROR, _NONE 등 나머지 타입
|
default -> null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
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.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return switch (cell.getCachedFormulaResultType()) {
|
||||||
|
case STRING -> cell.getStringCellValue();
|
||||||
|
case NUMERIC -> getStringNumericCell(cell, cell.getNumericCellValue());
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user