Browse Source

excel导入导出优化

wangliang 4 years ago
parent
commit
0dcaaa9e5c

+ 18 - 0
themis-business/src/main/java/com/qmth/themis/business/dto/ExamStudentImportDto.java

@@ -1,6 +1,7 @@
 package com.qmth.themis.business.dto;
 
 import com.qmth.themis.business.annotation.ExcelNotNull;
+import com.qmth.themis.business.annotation.ExcelNote;
 
 import java.io.Serializable;
 
@@ -14,17 +15,34 @@ import java.io.Serializable;
 public class ExamStudentImportDto implements Serializable {
 
     @ExcelNotNull
+    @ExcelNote(value = "姓名")
     private String name; //姓名
+
     @ExcelNotNull
+    @ExcelNote(value = "证件号")
     private String identity; //证件号
+
     @ExcelNotNull
+    @ExcelNote(value = "科目代码")
     private String courseCode; //科目代码
+
     @ExcelNotNull
+    @ExcelNote(value = "科目名称")
     private String courseName; //科目名称
+
+    @ExcelNote(value = "年级")
     private String grade; //年级
+
+    @ExcelNote(value = "教学班级")
     private String classNo; //教学班级
+
+    @ExcelNote(value = "场次")
     private String examActivityCode; //场次
+
+    @ExcelNote(value = "考场代码")
     private String roomCode; //考场代码
+
+    @ExcelNote(value = "考场名称")
     private String roomName; //考场名称
 
     public String getName() {

+ 0 - 13
themis-business/src/main/java/com/qmth/themis/business/dto/MarkResultSimpleExportDto.java

@@ -17,23 +17,18 @@ import java.io.Serializable;
  */
 public class MarkResultSimpleExportDto implements Serializable {
 
-    @ApiModelProperty(value = "批次名称")
     @ExcelNote(value = "批次名称")
     private String examName;
 
     @ExcelNote(value = "姓名")
-    @ApiModelProperty(name = "姓名")
     private String name;
 
-    @ApiModelProperty(name = "证件号")
     @ExcelNote(value = "证件号")
     private String identity;
 
-    @ApiModelProperty(name = "科目代码")
     @ExcelNote(value = "科目代码")
     private String courseCode;
 
-    @ApiModelProperty(name = "科目名称")
     @ExcelNote(value = "科目名称")
     private String courseName;
 
@@ -43,35 +38,27 @@ public class MarkResultSimpleExportDto implements Serializable {
     @ExcelNote(value = "考场名称")
     private String roomName; //考场名称
 
-    @ApiModelProperty(value = "年级")
     @ExcelNote(value = "年级")
     private String grade;
 
-    @ApiModelProperty(value = "教学班级")
     @ExcelNote(value = "教学班级")
     private String classNo;
 
-    @ApiModelProperty(value = "是否缺考")
     @ExcelNote(value = "是否缺考")
     private String examAbsent;
 
-    @ApiModelProperty(value = "违纪次数")
     @ExcelNote(value = "违纪次数")
     private String breachCount;
 
-    @ApiModelProperty(value = "考试次数")
     @ExcelNote(value = "考试次数")
     private String examCount;
 
-    @ApiModelProperty(value = "客观分")
     @ExcelNote(value = "客观分")
     private String objectiveScore;
 
-    @ApiModelProperty(value = "主观分")
     @ExcelNote(value = "主观分")
     private String subjectiveScore;
 
-    @ApiModelProperty(value = "总分")
     @ExcelNote(value = "总分")
     private String sumScore;
 

+ 0 - 13
themis-business/src/main/java/com/qmth/themis/business/dto/MarkResultStandardExportDto.java

@@ -18,23 +18,18 @@ import java.util.Map;
  */
 public class MarkResultStandardExportDto implements Serializable{
 
-    @ApiModelProperty(value = "批次名称")
     @ExcelNote(value = "批次名称")
     private String examName;
 
     @ExcelNote(value = "姓名")
-    @ApiModelProperty(name = "姓名")
     private String name;
 
-    @ApiModelProperty(name = "证件号")
     @ExcelNote(value = "证件号")
     private String identity;
 
-    @ApiModelProperty(name = "科目代码")
     @ExcelNote(value = "科目代码")
     private String courseCode;
 
-    @ApiModelProperty(name = "科目名称")
     @ExcelNote(value = "科目名称")
     private String courseName;
 
@@ -44,35 +39,27 @@ public class MarkResultStandardExportDto implements Serializable{
     @ExcelNote(value = "考场名称")
     private String roomName; //考场名称
 
-    @ApiModelProperty(value = "年级")
     @ExcelNote(value = "年级")
     private String grade;
 
-    @ApiModelProperty(value = "教学班级")
     @ExcelNote(value = "教学班级")
     private String classNo;
 
-    @ApiModelProperty(value = "是否缺考")
     @ExcelNote(value = "是否缺考")
     private String examAbsent;
 
-    @ApiModelProperty(value = "违纪次数")
     @ExcelNote(value = "违纪次数")
     private String breachCount;
 
-    @ApiModelProperty(value = "考试次数")
     @ExcelNote(value = "考试次数")
     private String examCount;
 
-    @ApiModelProperty(value = "客观分")
     @ExcelNote(value = "客观分")
     private String objectiveScore;
 
-    @ApiModelProperty(value = "主观分")
     @ExcelNote(value = "主观分")
     private String subjectiveScore;
 
-    @ApiModelProperty(value = "总分")
     @ExcelNote(value = "总分")
     private String sumScore;
 

+ 2 - 0
themis-business/src/main/java/com/qmth/themis/business/dto/RoomCodeExportDto.java

@@ -14,8 +14,10 @@ public class RoomCodeExportDto {
 
     @ExcelNote(value = "考场代码")
     private String roomCode; //考场代码
+
     @ExcelNote(value = "考场名称")
     private String roomName; //考场名称
+
     @ExcelNote(value = "监考老师")
     private String name;//监考老师
 

+ 10 - 0
themis-business/src/main/java/com/qmth/themis/business/dto/RoomCodeImportDto.java

@@ -1,6 +1,7 @@
 package com.qmth.themis.business.dto;
 
 import com.qmth.themis.business.annotation.ExcelNotNull;
+import com.qmth.themis.business.annotation.ExcelNote;
 
 import java.io.Serializable;
 
@@ -14,11 +15,20 @@ import java.io.Serializable;
 public class RoomCodeImportDto implements Serializable {
 
     @ExcelNotNull
+    @ExcelNote(value = "考场代码")
     private String roomCode; //考场代码
+
     @ExcelNotNull
+    @ExcelNote(value = "考场名称")
     private String roomName; //考场名称
+
+    @ExcelNote(value = "监考账号1")
     private String teacher1; //监考帐号1
+
+    @ExcelNote(value = "监考帐号2")
     private String teacher2; //监考帐号2
+
+    @ExcelNote(value = "监考帐号3")
     private String teacher3; //监考帐号3
 
     public String getRoomCode() {

+ 0 - 79
themis-business/src/main/java/com/qmth/themis/business/enums/AttachmentEnum.java

@@ -1,79 +0,0 @@
-package com.qmth.themis.business.enums;
-
-import java.util.Objects;
-
-/**
- * @Description: 附件enum
- * @Param:
- * @return:
- * @Author: wangliang
- * @Date: 2020/4/18
- */
-public enum AttachmentEnum {
-
-    name("姓名"),
-
-    identity("证件号"),
-
-    courseCode("科目代码"),
-
-    courseName("科目名称"),
-
-    grade("年级"),
-
-    classNo("教学班级"),
-
-    examActivityCode("场次"),
-
-    roomCode("考场代码"),
-
-    roomName("考场名称"),
-
-    teacher1("监考账号1"),
-
-    teacher2("监考账号2"),
-
-    teacher3("监考账号3"),
-
-    extendColumn("扩展字段");
-
-    private String code;
-
-    private AttachmentEnum(String code) {
-        this.code = code;
-    }
-
-    /**
-     * 状态转换 toName
-     *
-     * @param value
-     * @return
-     */
-    public static String convertToName(String value) {
-        for (AttachmentEnum e : AttachmentEnum.values()) {
-            if (Objects.equals(value.trim(), e.code)) {
-                return e.name();
-            }
-        }
-        return null;
-    }
-
-    /**
-     * 状态转换 toCode
-     *
-     * @param value
-     * @return
-     */
-    public static String convertToCode(String value) {
-        for (AttachmentEnum e : AttachmentEnum.values()) {
-            if (Objects.equals(value.trim(), e.name())) {
-                return e.code;
-            }
-        }
-        return null;
-    }
-
-    public String getCode() {
-        return code;
-    }
-}

+ 16 - 27
themis-business/src/main/java/com/qmth/themis/business/util/ExcelUtil.java

@@ -1,7 +1,7 @@
 package com.qmth.themis.business.util;
 
 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.exception.BusinessException;
 import org.apache.poi.ss.usermodel.Cell;
@@ -16,7 +16,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
 
 /**
  * @Description: excel util
@@ -37,7 +39,8 @@ public class ExcelUtil {
      * @return
      * @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 {
             log.info("开始读取excel里的数据");
             long start = System.currentTimeMillis();
@@ -63,35 +66,18 @@ public class ExcelUtil {
                         int firstcell = row.getFirstCellNum();//从第二行开始获取
                         //获取这一行的最后一列
                         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++) {
                             //获取第j列
                             Cell cell = row.getCell(j);
                             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 {
                                 if (Objects.nonNull(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) {
                     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());
-                    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 {
                     throw new BusinessException(e.getMessage());
                 }
@@ -167,8 +155,9 @@ public class ExcelUtil {
             Field field = fields[i];
             field.setAccessible(true);
             Annotation annotation = field.getAnnotation(ExcelNotNull.class);
+            ExcelNote note = field.getAnnotation(ExcelNote.class);
             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;