ezhizao_zx 1 year ago
parent
commit
e4ba40bd18

+ 18 - 0
src/main/java/cn/ezhizao/common/utils/poi/ExcelUtil.java

@@ -1688,4 +1688,22 @@ public class ExcelUtil<T>
         }
         return method;
     }
+
+    //读取表头
+    public List<String> readExcelHeaders(InputStream inputStream) throws IOException {
+        List<String> headers = new ArrayList<>();
+
+        Workbook workbook = WorkbookFactory.create(inputStream);
+        Sheet sheet = workbook.getSheetAt(0); // Assuming the header is in the first row
+
+        Row headerRow = sheet.getRow(0);
+
+        Iterator<Cell> cellIterator = headerRow.cellIterator();
+        while (cellIterator.hasNext()) {
+            Cell cell = cellIterator.next();
+            headers.add(cell.getStringCellValue());
+        }
+
+        return headers;
+    }
 }

+ 26 - 1
src/main/java/cn/ezhizao/project/system/controller/SysUserController.java

@@ -4,6 +4,7 @@ import cn.ezhizao.common.constant.UserConstants;
 import cn.ezhizao.common.utils.SecurityUtils;
 import cn.ezhizao.common.utils.StringUtils;
 import cn.ezhizao.common.utils.poi.ExcelUtil;
+import cn.ezhizao.framework.aspectj.lang.annotation.Excel;
 import cn.ezhizao.framework.aspectj.lang.annotation.Log;
 import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
 import cn.ezhizao.framework.web.controller.BaseController;
@@ -28,6 +29,8 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -92,13 +95,35 @@ public class SysUserController extends BaseController
     {
         ExcelUtil<SysUser> util = new ExcelUtil<>(SysUser.class);
         List<SysUser> userList = util.importExcel(file.getInputStream());
+        String operName = getUsername();
+        // 获取 入Excel 表头字段
+        List<String> excelHeaders = util.readExcelHeaders(file.getInputStream());
+        //获取实体类字段
+        List<String> names=getExcelAnnotatedFields(SysUser.class);
+        //如果不存在模板中的字段,返回错误
+        if (!names.containsAll(excelHeaders)) {
+            return error("导入数据与模板不匹配!");
+        }
         SysUser user = new SysUser();
         setTenantId(user);
-        String operName = getUsername();
+
         String message = userService.importUser(userList, updateSupport, operName,user.getTenantId());
         return success(message);
     }
 
+    public  List<String> getExcelAnnotatedFields(Class<?> clazz) {
+        Field[] fields = clazz.getDeclaredFields();
+        List<String> result = new ArrayList<>();
+        for (Field field : fields) {
+            if (field.isAnnotationPresent(Excel.class)) {
+                Excel excelAnnotation = field.getAnnotation(Excel.class);
+                String fieldName = excelAnnotation.name();
+                result.add(fieldName);
+            }
+        }
+        return result;
+    }
+
     @PostMapping("/importTemplate")
     public void importTemplate(HttpServletResponse response)
     {

+ 1 - 1
src/main/java/cn/ezhizao/project/system/service/impl/SysUserServiceImpl.java

@@ -580,7 +580,7 @@ public class SysUserServiceImpl implements ISysUserService
                     failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 用户名称应为手机号");
                     continue;
                 }
-                if(user.getNickName().equals(" ")||user.getNickName()==null){
+                if(user.getNickName().equals("")||user.getNickName()==null){
                     failureNum++;
                     failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + "用户昵称不能为空");
                     continue;