导入读取excel数据工具类优化
This commit is contained in:
@ -13,6 +13,8 @@ import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@ -146,6 +148,20 @@ public class ExcelDynamicReader {
|
||||
* 获取单元格原始值
|
||||
*/
|
||||
private static Object getRawCellValue(Cell cell, CellType cellType) {
|
||||
// switch (cellType) {
|
||||
// case STRING:
|
||||
// return cell.getStringCellValue().trim();
|
||||
// case NUMERIC:
|
||||
// if (DateUtil.isCellDateFormatted(cell)) {
|
||||
// return cell.getDateCellValue();
|
||||
// } else {
|
||||
// return cell.getNumericCellValue();
|
||||
// }
|
||||
// case BOOLEAN:
|
||||
// return cell.getBooleanCellValue();
|
||||
// default:
|
||||
// return "";
|
||||
// }
|
||||
switch (cellType) {
|
||||
case STRING:
|
||||
return cell.getStringCellValue().trim();
|
||||
@ -153,7 +169,13 @@ public class ExcelDynamicReader {
|
||||
if (DateUtil.isCellDateFormatted(cell)) {
|
||||
return cell.getDateCellValue();
|
||||
} else {
|
||||
return cell.getNumericCellValue();
|
||||
double numericValue = cell.getNumericCellValue();
|
||||
// 关键判断:如果是整数形式的数字(如1.0),返回Long;否则返回Double
|
||||
if (numericValue == Math.floor(numericValue) && !Double.isInfinite(numericValue)) {
|
||||
return (long) numericValue; // 1.0 → 1(Long类型)
|
||||
} else {
|
||||
return numericValue; // 1.5 → 1.5(Double类型)
|
||||
}
|
||||
}
|
||||
case BOOLEAN:
|
||||
return cell.getBooleanCellValue();
|
||||
@ -166,12 +188,56 @@ public class ExcelDynamicReader {
|
||||
* 从字符串转换到目标类型
|
||||
*/
|
||||
private static Object convertFromString(String value, Class<?> targetType) {
|
||||
// if (value.isEmpty()) {
|
||||
// return getDefaultValue(targetType);
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// if (targetType == Integer.class || targetType == int.class) {
|
||||
// return Integer.parseInt(value);
|
||||
// } else if (targetType == Long.class || targetType == long.class) {
|
||||
// return Long.parseLong(value);
|
||||
// } else if (targetType == Double.class || targetType == double.class) {
|
||||
// return Double.parseDouble(value);
|
||||
// } else if (targetType == Float.class || targetType == float.class) {
|
||||
// return Float.parseFloat(value);
|
||||
// } else if (targetType == BigDecimal.class) {
|
||||
// return new BigDecimal(value);
|
||||
// } else if (targetType == Boolean.class || targetType == boolean.class) {
|
||||
// return "true".equalsIgnoreCase(value) || "1".equals(value);
|
||||
// } else if (targetType == LocalDate.class) {
|
||||
// // 支持多种日期格式
|
||||
// String[] patterns = {"yyyy-MM-dd", "yyyy/MM/dd", "MM/dd/yyyy", "dd-MM-yyyy"};
|
||||
// for (String pattern : patterns) {
|
||||
// try {
|
||||
// return LocalDate.parse(value, DateTimeFormatter.ofPattern(pattern, Locale.US));
|
||||
// } catch (Exception e) {
|
||||
// // 尝试下一种格式
|
||||
// }
|
||||
// }
|
||||
// // 最后尝试默认格式
|
||||
// return LocalDate.parse(value);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// throw new RuntimeException("无法将字符串 '" + value + "' 转换为 " + targetType.getSimpleName(), e);
|
||||
// }
|
||||
//
|
||||
// return value;
|
||||
|
||||
if (value.isEmpty()) {
|
||||
return getDefaultValue(targetType);
|
||||
}
|
||||
|
||||
try {
|
||||
if (targetType == Integer.class || targetType == int.class) {
|
||||
if (targetType == BigDecimal.class) {
|
||||
// 处理带千位分隔符的数字(如"1,234.56")
|
||||
if (value.contains(",")) {
|
||||
NumberFormat format = NumberFormat.getInstance(Locale.US);
|
||||
return new BigDecimal(format.parse(value).toString());
|
||||
} else {
|
||||
return new BigDecimal(value);
|
||||
}
|
||||
} else if (targetType == Integer.class || targetType == int.class) {
|
||||
return Integer.parseInt(value);
|
||||
} else if (targetType == Long.class || targetType == long.class) {
|
||||
return Long.parseLong(value);
|
||||
@ -179,12 +245,9 @@ public class ExcelDynamicReader {
|
||||
return Double.parseDouble(value);
|
||||
} else if (targetType == Float.class || targetType == float.class) {
|
||||
return Float.parseFloat(value);
|
||||
} else if (targetType == BigDecimal.class) {
|
||||
return new BigDecimal(value);
|
||||
} else if (targetType == Boolean.class || targetType == boolean.class) {
|
||||
return "true".equalsIgnoreCase(value) || "1".equals(value);
|
||||
} else if (targetType == LocalDate.class) {
|
||||
// 支持多种日期格式
|
||||
String[] patterns = {"yyyy-MM-dd", "yyyy/MM/dd", "MM/dd/yyyy", "dd-MM-yyyy"};
|
||||
for (String pattern : patterns) {
|
||||
try {
|
||||
@ -193,9 +256,10 @@ public class ExcelDynamicReader {
|
||||
// 尝试下一种格式
|
||||
}
|
||||
}
|
||||
// 最后尝试默认格式
|
||||
return LocalDate.parse(value);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException("字符串格式错误,无法转换为数字: " + value, e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("无法将字符串 '" + value + "' 转换为 " + targetType.getSimpleName(), e);
|
||||
}
|
||||
@ -207,6 +271,58 @@ public class ExcelDynamicReader {
|
||||
* 从数字转换到目标类型
|
||||
*/
|
||||
private static Object convertFromNumber(Number number, Class<?> targetType) {
|
||||
// if (targetType == Integer.class || targetType == int.class) {
|
||||
// return number.intValue();
|
||||
// } else if (targetType == Long.class || targetType == long.class) {
|
||||
// return number.longValue();
|
||||
// } else if (targetType == Double.class || targetType == double.class) {
|
||||
// return number.doubleValue();
|
||||
// } else if (targetType == Float.class || targetType == float.class) {
|
||||
// return number.floatValue();
|
||||
// } else if (targetType == BigDecimal.class) {
|
||||
// return BigDecimal.valueOf(number.doubleValue());
|
||||
// } else if (targetType == String.class) {
|
||||
// return number.toString();
|
||||
// } else if (targetType == Boolean.class || targetType == boolean.class) {
|
||||
// return number.doubleValue() != 0;
|
||||
// }
|
||||
//
|
||||
// return number;
|
||||
// 优先处理BigDecimal目标类型
|
||||
if (targetType == BigDecimal.class) {
|
||||
if (number instanceof Long) {
|
||||
return BigDecimal.valueOf(number.longValue()); // Long → BigDecimal
|
||||
} else if (number instanceof Double) {
|
||||
return BigDecimal.valueOf(number.doubleValue()); // Double → BigDecimal
|
||||
} else {
|
||||
return new BigDecimal(number.toString()); // 其他数字类型 → BigDecimal
|
||||
}
|
||||
}
|
||||
|
||||
// 处理整数类型(Long)
|
||||
if (number instanceof Long) {
|
||||
long longValue = (Long) number;
|
||||
if (targetType == Integer.class || targetType == int.class) {
|
||||
return (int) longValue;
|
||||
} else if (targetType == String.class) {
|
||||
return Long.toString(longValue);
|
||||
}
|
||||
return longValue;
|
||||
}
|
||||
|
||||
// 处理浮点数类型(Double)
|
||||
if (number instanceof Double) {
|
||||
double doubleValue = (Double) number;
|
||||
if ((targetType == Integer.class || targetType == int.class) &&
|
||||
doubleValue == Math.floor(doubleValue)) {
|
||||
return (int) doubleValue;
|
||||
} else if ((targetType == Long.class || targetType == long.class) &&
|
||||
doubleValue == Math.floor(doubleValue)) {
|
||||
return (long) doubleValue;
|
||||
}
|
||||
}
|
||||
|
||||
// 通用数字转换
|
||||
if (targetType == Integer.class || targetType == int.class) {
|
||||
return number.intValue();
|
||||
} else if (targetType == Long.class || targetType == long.class) {
|
||||
@ -215,8 +331,6 @@ public class ExcelDynamicReader {
|
||||
return number.doubleValue();
|
||||
} else if (targetType == Float.class || targetType == float.class) {
|
||||
return number.floatValue();
|
||||
} else if (targetType == BigDecimal.class) {
|
||||
return BigDecimal.valueOf(number.doubleValue());
|
||||
} else if (targetType == String.class) {
|
||||
return number.toString();
|
||||
} else if (targetType == Boolean.class || targetType == boolean.class) {
|
||||
|
||||
Reference in New Issue
Block a user