Browse Source

BUG修复:Excel文件解析错误BUG修复

GDW 1 week ago
parent
commit
deab3ff6f2

+ 91 - 9
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysUserController.java

@@ -16,6 +16,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.catalina.User;
 import org.apache.commons.lang.StringUtils;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.apache.shiro.authz.annotation.RequiresRoles;
@@ -46,6 +47,7 @@ import org.jeecgframework.poi.excel.def.NormalExcelConstants;
 import org.jeecgframework.poi.excel.entity.ExportParams;
 import org.jeecgframework.poi.excel.entity.ImportParams;
 import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
+import org.jeecgframework.poi.util.PoiPublicUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
@@ -55,7 +57,7 @@ import org.springframework.web.servlet.ModelAndView;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+import java.io.*;
 import java.net.URLDecoder;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -623,7 +625,60 @@ public class SysUserController {
                 String[] expectedHeaders = {"登录账号", "真实姓名", "性别", "电话", "工号", "所属部门"};
                 boolean isHeaderValid = checkHeader(headerRow, expectedHeaders);
 
-                List<SysUser> listSysUsers = ExcelImportUtil.importExcel(file.getInputStream(), SysUser.class, params);
+                Workbook book = WorkbookFactory.create(file.getInputStream());
+                Sheet tempSheet = book.getSheetAt(0);
+                int lastRowNum = tempSheet.getLastRowNum();
+                int startRowNum = 3;
+
+                // 创建一个新的Workbook和Sheet
+                Workbook newBook = new XSSFWorkbook();
+                Sheet newSheet = newBook.createSheet("Filtered Data");
+
+                int newRowNum = 0; // 新Sheet的行号
+
+                // 复制前三行
+                for (int i = 0; i < startRowNum; i++) {
+                    Row row = tempSheet.getRow(i);
+                    if (row != null) {
+                        Row newRow = newSheet.createRow(newRowNum++);
+                        for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
+                            Cell cell = row.getCell(j);
+                            if (cell != null) {
+                                Cell newCell = newRow.createCell(j);
+                                copyCell(cell, newCell);
+                            }
+                        }
+                    }
+                }
+
+                for (int i = startRowNum; i <= lastRowNum; i++) {
+                    Row row = tempSheet.getRow(i);
+                    if (row != null) {
+                        short firstCellNum = row.getFirstCellNum();
+                        if (firstCellNum != -1) {
+                            // 复制行到新的Sheet
+                            Row newRow = newSheet.createRow(newRowNum++);
+                            for (int j = firstCellNum; j <= row.getLastCellNum(); j++) {
+                                Cell cell = row.getCell(j);
+                                if (cell != null) {
+                                    Cell newCell = newRow.createCell(j);
+                                    copyCell(cell, newCell);
+                                }
+                            }
+                        }
+                    }
+                }
+                // 将新的Workbook写入ByteArrayOutputStream
+                ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
+                newBook.write(byteArrayOut);
+                newBook.close();
+                book.close();
+
+                // 获取InputStream
+                InputStream inputStream = new ByteArrayInputStream(byteArrayOut.toByteArray());
+                byteArrayOut.close();
+
+                List<SysUser> listSysUsers = ExcelImportUtil.importExcel(inputStream, SysUser.class, params);
 
                 if (!isHeaderValid) {
                     errorMessage.add("文件 " + fileName + " 格式不正确");
@@ -631,14 +686,11 @@ public class SysUserController {
                     throw new IllegalArgumentException("文件格式不正确");
                 }
                 for (int i = 0; i < listSysUsers.size(); i++) {
+
                     SysUser sysUserExcel = listSysUsers.get(i);
                     String depart = sysUserExcel.getDepartmentId();
-                    if (depart.contains("/")) {
-                        String[] departNames = depart.split("/");
-                        List<SysDepartTreeModel> list = sysDepartService.queryMyDeptTreeList(null);
-                        SysDepartTreeModel nodeByPath = findNodeByPath(list, departNames);
-                        sysUserExcel.setDepartmentId(nodeByPath.getId());
-                    }
+
+
                     if (StringUtils.isBlank(sysUserExcel.getPassword())) {
                         // 密码默认为 “123456”
                         sysUserExcel.setPassword("123456");
@@ -653,6 +705,15 @@ public class SysUserController {
                     String passwordEncode = PasswordUtil.encrypt(sysUserExcel.getUsername(), sysUserExcel.getPassword(), salt);
                     sysUserExcel.setPassword(passwordEncode);
                     try {
+                        if (depart.contains("/")) {
+                            String[] departNames = depart.split("/");
+                            List<SysDepartTreeModel> list = sysDepartService.queryMyDeptTreeList(null);
+                            SysDepartTreeModel nodeByPath = findNodeByPath(list, departNames);
+                            if (ObjectUtil.isEmpty(nodeByPath)) {
+                                throw new IllegalArgumentException("组织不存在");
+                            }
+                            sysUserExcel.setDepartmentId(nodeByPath.getId());
+                        }
 
                         if (ObjectUtil.isNull(sysUserExcel.getUsername()) || sysUserExcel.getUsername().isEmpty()) {
 //                            errorMessage.add("第 " + i+1 + " 行:用户名为空。");
@@ -706,7 +767,9 @@ public class SysUserController {
                         } else if (message.contains("用户名为空")) {
                             errorMessage.add("第 " + lineNumber + " 行:用户名为空。");
                         } else if (message.contains("用户真实姓名为空")) {
-                            errorMessage.add("第 " + lineNumber + " 行:用户真实姓名为空。");
+                            errorMessage.add("第 " + lineNumber + " 行:用户真实姓名为空。用户名:" + sysUserExcel.getUsername());
+                        } else if (message.contains("组织不存在")) {
+                            errorMessage.add("第 " + lineNumber + " 行:组织不存在。用户名:" + sysUserExcel.getUsername());
                         } else {
                             errorMessage.add("第 " + lineNumber + " 行:未知错误,忽略导入");
                             log.error(e.getMessage(), e);
@@ -2146,4 +2209,23 @@ public class SysUserController {
         return null;
     }
 
+    private void copyCell(Cell oldCell, Cell newCell) {
+        switch (oldCell.getCellType()) {
+            case STRING:
+                newCell.setCellValue(oldCell.getStringCellValue());
+                break;
+            case NUMERIC:
+                newCell.setCellValue(oldCell.getNumericCellValue());
+                break;
+            case BOOLEAN:
+                newCell.setCellValue(oldCell.getBooleanCellValue());
+                break;
+            case FORMULA:
+                newCell.setCellFormula(oldCell.getCellFormula());
+                break;
+            default:
+                break;
+        }
+    }
+
 }