|
@@ -1,7 +1,7 @@
|
|
package com.qmth.themis.business.util;
|
|
package com.qmth.themis.business.util;
|
|
|
|
|
|
import com.qmth.themis.business.annotation.ExcelNotNull;
|
|
import com.qmth.themis.business.annotation.ExcelNotNull;
|
|
-import com.qmth.themis.business.enums.AttachmentEnum;
|
|
|
|
|
|
+import com.qmth.themis.business.annotation.ExcelNote;
|
|
import com.qmth.themis.common.enums.ExceptionResultEnum;
|
|
import com.qmth.themis.common.enums.ExceptionResultEnum;
|
|
import com.qmth.themis.common.exception.BusinessException;
|
|
import com.qmth.themis.common.exception.BusinessException;
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
@@ -16,7 +16,9 @@ import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
import java.lang.annotation.Annotation;
|
|
import java.lang.annotation.Annotation;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @Description: excel util
|
|
* @Description: excel util
|
|
@@ -37,7 +39,8 @@ public class ExcelUtil {
|
|
* @return
|
|
* @return
|
|
* @throws IOException
|
|
* @throws IOException
|
|
*/
|
|
*/
|
|
- public static List<LinkedMultiValueMap<Integer, Object>> excelReader(InputStream inputStream, List<Class<?>> clazz, ExcelCallback callback) throws IOException {
|
|
|
|
|
|
+ public static List<LinkedMultiValueMap<Integer, Object>> excelReader(InputStream inputStream, List<Class<?>> clazz, ExcelCallback callback) throws IOException, NoSuchFieldException {
|
|
|
|
+ Object o = null;
|
|
try {
|
|
try {
|
|
log.info("开始读取excel里的数据");
|
|
log.info("开始读取excel里的数据");
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
@@ -63,35 +66,18 @@ public class ExcelUtil {
|
|
int firstcell = row.getFirstCellNum();//从第二行开始获取
|
|
int firstcell = row.getFirstCellNum();//从第二行开始获取
|
|
//获取这一行的最后一列
|
|
//获取这一行的最后一列
|
|
int lastcell = row.getLastCellNum();
|
|
int lastcell = row.getLastCellNum();
|
|
- Object o = clazz.get(y).newInstance();
|
|
|
|
|
|
+ o = clazz.get(y).newInstance();
|
|
|
|
+ Field[] fields = o.getClass().getDeclaredFields();
|
|
for (int j = firstcell; j < lastcell; j++) {
|
|
for (int j = firstcell; j < lastcell; j++) {
|
|
//获取第j列
|
|
//获取第j列
|
|
Cell cell = row.getCell(j);
|
|
Cell cell = row.getCell(j);
|
|
if (i == 0) {
|
|
if (i == 0) {
|
|
- if (!Objects.equals(AttachmentEnum.convertToName(String.valueOf(cell)), AttachmentEnum.extendColumn.name())) {
|
|
|
|
- columnNameList.add(y, AttachmentEnum.convertToName(String.valueOf(cell)));
|
|
|
|
- } else {
|
|
|
|
- columnNameList.add(y, String.valueOf(cell));
|
|
|
|
- }
|
|
|
|
|
|
+ columnNameList.add(y, String.valueOf(cell));
|
|
} else {
|
|
} else {
|
|
if (Objects.nonNull(cell)) {
|
|
if (Objects.nonNull(cell)) {
|
|
Object obj = convert(cell);
|
|
Object obj = convert(cell);
|
|
- Field field = null;
|
|
|
|
- String fieldName = AttachmentEnum.convertToCode(columnNameList.get(y).get(j));
|
|
|
|
- if (Objects.equals(fieldName, AttachmentEnum.extendColumn.getCode())) {
|
|
|
|
- field = o.getClass().getDeclaredField(AttachmentEnum.extendColumn.name());
|
|
|
|
- field.setAccessible(true);
|
|
|
|
- Map map = (Map) field.get(o);
|
|
|
|
- if (Objects.isNull(map)) {
|
|
|
|
- map = new LinkedHashMap<>();
|
|
|
|
- }
|
|
|
|
- map.put(columnNameList.get(y).get(j), obj);
|
|
|
|
- field.set(o, map);
|
|
|
|
- } else {
|
|
|
|
- field = o.getClass().getDeclaredField(columnNameList.get(y).get(j));
|
|
|
|
- field.setAccessible(true);
|
|
|
|
- field.set(o, obj);
|
|
|
|
- }
|
|
|
|
|
|
+ fields[j].setAccessible(true);
|
|
|
|
+ fields[j].set(o, obj);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -119,7 +105,9 @@ public class ExcelUtil {
|
|
if (errorColumn.indexOf("Can not set java.lang.String field") != -1 && errorColumn.indexOf("to java.lang.Long") != -1) {
|
|
if (errorColumn.indexOf("Can not set java.lang.String field") != -1 && errorColumn.indexOf("to java.lang.Long") != -1) {
|
|
String column = errorColumn.substring(errorColumn.indexOf("Can not set java.lang.String field") + 1, errorColumn.indexOf("to java.lang.Long"));
|
|
String column = errorColumn.substring(errorColumn.indexOf("Can not set java.lang.String field") + 1, errorColumn.indexOf("to java.lang.Long"));
|
|
column = column.substring(column.lastIndexOf(".") + 1, column.length());
|
|
column = column.substring(column.lastIndexOf(".") + 1, column.length());
|
|
- throw new BusinessException("excel列[" + AttachmentEnum.convertToCode(column) + "]为非文本格式");
|
|
|
|
|
|
+ Field field = o.getClass().getDeclaredField(column.trim());
|
|
|
|
+ ExcelNote note = field.getAnnotation(ExcelNote.class);
|
|
|
|
+ throw new BusinessException("excel列[" + note.value() + "]为非文本格式");
|
|
} else {
|
|
} else {
|
|
throw new BusinessException(e.getMessage());
|
|
throw new BusinessException(e.getMessage());
|
|
}
|
|
}
|
|
@@ -167,8 +155,9 @@ public class ExcelUtil {
|
|
Field field = fields[i];
|
|
Field field = fields[i];
|
|
field.setAccessible(true);
|
|
field.setAccessible(true);
|
|
Annotation annotation = field.getAnnotation(ExcelNotNull.class);
|
|
Annotation annotation = field.getAnnotation(ExcelNotNull.class);
|
|
|
|
+ ExcelNote note = field.getAnnotation(ExcelNote.class);
|
|
if (Objects.isNull(field.get(obj)) && Objects.nonNull(annotation)) {
|
|
if (Objects.isNull(field.get(obj)) && Objects.nonNull(annotation)) {
|
|
- excelErrorList.add(new ExcelError(index + 1, "excel第" + (sheetIndex + 1) + "个sheet第" + (index + 1) + "行[" + AttachmentEnum.convertToCode(field.getName()) + "]为空"));
|
|
|
|
|
|
+ excelErrorList.add(new ExcelError(index + 1, "excel第" + (sheetIndex + 1) + "个sheet第" + (index + 1) + "行[" + note.value() + "]为空"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return excelErrorList;
|
|
return excelErrorList;
|