Răsfoiți Sursa

扩展ExcelReader,在解析为对象时,按照定义注解验证表头,有非空列名缺失时抛出异常并返回缺少的列名

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 2 ani în urmă
părinte
comite
ccbc97a392

+ 21 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/exception/ColumnNameException.java

@@ -0,0 +1,21 @@
+package com.qmth.boot.tools.excel.exception;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.Collection;
+
+public class ColumnNameException extends RuntimeException {
+
+    private static final long serialVersionUID = 1861441092000446536L;
+
+    private Collection<String> errorNames;
+
+    public ColumnNameException(Collection<String> errorNames) {
+        super("Excel缺少列名:" + StringUtils.join(errorNames, ","));
+        this.errorNames = errorNames;
+    }
+
+    public Collection<String> getErrorNames() {
+        return errorNames;
+    }
+}

+ 9 - 1
tools-poi/src/main/java/com/qmth/boot/tools/excel/handler/impl/ObjectHandler.java

@@ -1,6 +1,7 @@
 package com.qmth.boot.tools.excel.handler.impl;
 
 import com.qmth.boot.tools.excel.convertor.ValueConvertors;
+import com.qmth.boot.tools.excel.exception.ColumnNameException;
 import com.qmth.boot.tools.excel.handler.RowDataHandler;
 import com.qmth.boot.tools.excel.listener.ObjectListener;
 import com.qmth.boot.tools.excel.model.FieldParam;
@@ -8,6 +9,7 @@ import com.qmth.boot.tools.excel.model.ObjectParam;
 import com.qmth.boot.tools.excel.model.RowData;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 public class ObjectHandler<E> implements RowDataHandler {
@@ -44,7 +46,13 @@ public class ObjectHandler<E> implements RowDataHandler {
     }
 
     private void buildColumnNames(RowData rowData) {
-        columnNames = rowData.getValues();
+        String[] names = rowData.getValues();
+        Collection<String> errors = objectParam.validate(names);
+        if (errors.isEmpty()) {
+            columnNames = names;
+        } else {
+            throw new ColumnNameException(errors);
+        }
     }
 
     private void convertObject(RowData rowData) {

+ 26 - 3
tools-poi/src/main/java/com/qmth/boot/tools/excel/model/ObjectParam.java

@@ -3,10 +3,12 @@ package com.qmth.boot.tools.excel.model;
 import com.qmth.boot.tools.excel.annotation.ExcelColumn;
 import com.qmth.boot.tools.excel.convertor.ValueConvertor;
 import com.qmth.boot.tools.excel.convertor.ValueConvertors;
+import org.apache.commons.lang3.ArrayUtils;
 
 import java.lang.reflect.Field;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
 
 public class ObjectParam extends HashMap<String, FieldParam> {
 
@@ -24,6 +26,8 @@ public class ObjectParam extends HashMap<String, FieldParam> {
 
     private String[] columnNames;
 
+    private List<FieldParam> fieldList;
+
     private ObjectParam(Class<?> objectType) {
         super();
 
@@ -48,12 +52,31 @@ public class ObjectParam extends HashMap<String, FieldParam> {
             }
         }
 
-        List<FieldParam> list = new ArrayList<>(values());
-        list.sort(Comparator.comparingInt(p -> p.getAnnotation().index()));
-        columnNames = list.stream().map(p -> p.getAnnotation().name()).toArray(String[]::new);
+        fieldList = new ArrayList<>(values());
+        fieldList.sort(Comparator.comparingInt(p -> p.getAnnotation().index()));
+        columnNames = fieldList.stream().map(p -> p.getAnnotation().name()).toArray(String[]::new);
     }
 
     public String[] getColumnNames() {
         return columnNames;
     }
+
+    /**
+     * 验证解析的列名,需要所有非空字段对应的列名存在
+     * 返回缺少的列名,不缺时返回集合为空
+     *
+     * @param columnNames
+     * @return
+     */
+    public Set<String> validate(String[] columnNames) {
+        Set<String> columnSet = fieldList.stream().filter(p -> !p.getAnnotation().nullable())
+                .map(p -> p.getAnnotation().name()).collect(Collectors.toCollection(LinkedHashSet::new));
+        if (ArrayUtils.isEmpty(columnNames)) {
+            return columnSet;
+        }
+        for (String name : columnNames) {
+            columnSet.remove(name);
+        }
+        return columnSet;
+    }
 }

+ 1 - 1
tools-poi/src/test/java/com/qmth/boot/test/tools/excel/read/ExcelReadTest.java

@@ -15,7 +15,7 @@ public class ExcelReadTest {
     public void testObjectRead() throws Exception {
         FileInputStream ins = new FileInputStream("/Users/luoshi/Downloads/test.xlsx");
         ObjectCollector<ExcelTestEntity> listener = new ObjectCollector<>();
-        ExcelReader.create(ExcelType.XLSX, ins, 2).readObject(ExcelTestEntity.class, listener);
+        ExcelReader.create(ExcelType.XLSX, ins, 0).readObject(ExcelTestEntity.class, listener);
         System.out.println(listener.getObjectList().size());
         ins.close();
     }