mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-12 20:16:07 +00:00
perf[storage]: 提高excel读取速度
This commit is contained in:
1 parent
94f0a6113f
commit
a30397f3a3
3 files changed
+31
-24
No files matched your search
@@ -145,6 +145,9 @@ public abstract class StringUtils {
|
||||
return cs == null || cs.length() == 0;
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(final CharSequence cs) {
|
||||
return !isEmpty(cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* StringUtils.isBlank(null)=true
|
||||
@@ -168,6 +171,10 @@ public abstract class StringUtils {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isNotBlank(final CharSequence cs) {
|
||||
return !isBlank(cs);
|
||||
}
|
||||
|
||||
public static String trim(String str) {
|
||||
if (StringUtils.isBlank(str)) {
|
||||
return EMPTY;
|
||||
|
||||
@@ -76,27 +76,17 @@ public class ExcelResourceReader implements IResourceReader {
|
||||
var instance = ReflectionUtils.newInstance(clazz);
|
||||
|
||||
var idCell = row.getCell(0);
|
||||
if (idCell == null || StringUtils.isBlank(CellUtils.getCellStringValue(idCell))) {
|
||||
if (StringUtils.isBlank(CellUtils.getCellStringValue(idCell))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var fieldInfo : fieldInfos) {
|
||||
var cell = row.getCell(fieldInfo.index);
|
||||
if (cell != null) {
|
||||
var content = CellUtils.getCellStringValue(cell);
|
||||
if (!StringUtils.isEmpty(content)) {
|
||||
inject(instance, fieldInfo.field, content);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果读的是id列的单元格,则判断当前id是否为空
|
||||
if (fieldInfo.field.isAnnotationPresent(Id.class)) {
|
||||
if (cell == null || StringUtils.isEmpty(CellUtils.getCellStringValue(cell))) {
|
||||
throw new RunException("静态资源[resource:{}]存在id未配置的项", clazz.getSimpleName());
|
||||
}
|
||||
var content = CellUtils.getCellStringValue(cell);
|
||||
if (StringUtils.isNotEmpty(content)) {
|
||||
inject(instance, fieldInfo.field, content);
|
||||
}
|
||||
}
|
||||
|
||||
result.add(instance);
|
||||
}
|
||||
return result;
|
||||
@@ -148,6 +138,13 @@ public class ExcelResourceReader implements IResourceReader {
|
||||
throw new RunException("资源类[class:{}]的声明属性[filed:{}]无法获取,请检查配置表的格式", clazz, field.getName());
|
||||
}
|
||||
|
||||
if (field.isAnnotationPresent(Id.class)) {
|
||||
var cellIndex = cellFieldMap.get(field.getName());
|
||||
if (cellIndex != 0) {
|
||||
throw new RunException("资源类[class:{}]的主键[Id:{}]必须放在Excel配置表的第一列,请检查配置表的格式", clazz, field.getName());
|
||||
}
|
||||
}
|
||||
|
||||
if (Modifier.isPublic(field.getModifiers())) {
|
||||
throw new RunException("因为静态资源类是不能被修改的,所以资源类[class:{}]的属性[filed:{}]不能被public修饰,请改为private修饰", clazz, field.getName());
|
||||
}
|
||||
|
||||
@@ -24,25 +24,31 @@ import org.apache.poi.ss.usermodel.FormulaError;
|
||||
* @version 3.0
|
||||
*/
|
||||
public abstract class CellUtils {
|
||||
|
||||
public static String getCellStringValue(Cell cell) {
|
||||
if (cell == null) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
return getCellValue(cell).toString().trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单元格值
|
||||
*
|
||||
* @return 值,类型可能为:Date、Double、Boolean、String
|
||||
*/
|
||||
public static Object getCellValue(Cell cell) {
|
||||
if (cell == null) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
return getCellValue(cell, cell.getCellType());
|
||||
}
|
||||
|
||||
public static String getCellStringValue(Cell cell) {
|
||||
return getCellValue(cell).toString().trim();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取单元格值<br>
|
||||
* 如果单元格值为数字格式,则判断其格式中是否有小数部分,无则返回Long类型,否则返回Double类型
|
||||
*/
|
||||
public static Object getCellValue(Cell cell, CellType cellType) {
|
||||
private static Object getCellValue(Cell cell, CellType cellType) {
|
||||
Object value;
|
||||
switch (cellType) {
|
||||
case NUMERIC:
|
||||
@@ -94,10 +100,7 @@ public abstract class CellUtils {
|
||||
// 普通数字
|
||||
if (null != format && !format.contains(StringUtils.PERIOD)) {
|
||||
var longValue = (long) value;
|
||||
if (longValue == value) {
|
||||
// 对于无小数部分的数字类型,转为Long
|
||||
return longValue;
|
||||
}
|
||||
// 对于无小数部分的数字类型,转为Long
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user