Browse Source

修改:使用封装类

GDW 1 week ago
parent
commit
1c5d885f06

+ 8 - 3
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysDepartController.java

@@ -27,6 +27,7 @@ import org.jeecg.modules.system.model.SysDepartTreeModel;
 import org.jeecg.modules.system.service.ISysDepartService;
 import org.jeecg.modules.system.service.ISysUserDepartService;
 import org.jeecg.modules.system.service.ISysUserService;
+import org.jeecg.modules.system.util.ExcelImportHandleUtil;
 import org.jeecg.modules.system.vo.SysDepartExportVo;
 import org.jeecg.modules.system.vo.lowapp.ExportDepartVo;
 import org.jeecgframework.poi.excel.ExcelImportUtil;
@@ -45,6 +46,7 @@ import org.springframework.web.servlet.ModelAndView;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -419,7 +421,8 @@ public class SysDepartController {
 			params.setHeadRows(1);
 			params.setNeedSave(true);
 			try {
-				listSysDeparts = ExcelImportUtil.importExcel(file.getInputStream(), SysDepartExportVo.class, params);
+				InputStream inputStream = ExcelImportHandleUtil.fileStreamProcessing(file, 3, 0);
+				listSysDeparts = ExcelImportUtil.importExcel(inputStream, SysDepartExportVo.class, params);
 				sysDepartService.initializationImportSysDepart(listSysDeparts, errorMessageList);
 				return ImportExcelUtil.imporReturnRes(errorMessageList.size(), listSysDeparts.size() - errorMessageList.size(), errorMessageList);
 			} catch (Exception e) {
@@ -504,7 +507,8 @@ public class SysDepartController {
 				//update-end---author:wangshuai---date:2023-10-20---for: 注释掉原来的导入部门的逻辑---
 				
 				//update-begin---author:wangshuai---date:2023-10-19---for:【QQYUN-5482】系统的部门导入导出也可以改成敲敲云模式的部门路径---
-				listSysDeparts = ExcelImportUtil.importExcel(file.getInputStream(), SysDepartExportVo.class, params);
+				InputStream inputStream = ExcelImportHandleUtil.fileStreamProcessing(file, 3, 0);
+				listSysDeparts = ExcelImportUtil.importExcel(inputStream, SysDepartExportVo.class, params);
 				sysDepartService.importSysDepart(listSysDeparts,errorMessageList);
 				//update-end---author:wangshuai---date:2023-10-19---for:【QQYUN-5482】系统的部门导入导出也可以改成敲敲云模式的部门路径---
 				
@@ -700,7 +704,8 @@ public class SysDepartController {
 			params.setHeadRows(1);
 			params.setNeedSave(true);
 			try {
-				listSysDeparts = ExcelImportUtil.importExcel(file.getInputStream(), ExportDepartVo.class, params);
+				InputStream inputStream = ExcelImportHandleUtil.fileStreamProcessing(file, 3, 0);
+				listSysDeparts = ExcelImportUtil.importExcel(inputStream, ExportDepartVo.class, params);
 				sysDepartService.importExcel(listSysDeparts,errorMessageList);
 				//清空部门缓存
 				Set keys3 = redisTemplate.keys(CacheConstant.SYS_DEPARTS_CACHE + "*");

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

@@ -38,6 +38,7 @@ import org.jeecg.modules.system.model.DepartIdModel;
 import org.jeecg.modules.system.model.SysDepartTreeModel;
 import org.jeecg.modules.system.model.SysUserSysDepartModel;
 import org.jeecg.modules.system.service.*;
+import org.jeecg.modules.system.util.ExcelImportHandleUtil;
 import org.jeecg.modules.system.vo.SysDepartUsersVO;
 import org.jeecg.modules.system.vo.SysUserRoleVO;
 import org.jeecg.modules.system.vo.lowapp.DepartAndUserInfo;
@@ -625,58 +626,8 @@ public class SysUserController {
                 String[] expectedHeaders = {"登录账号", "真实姓名", "性别", "电话", "工号", "所属部门"};
                 boolean isHeaderValid = checkHeader(headerRow, expectedHeaders);
 
-                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 = ExcelImportHandleUtil.fileStreamProcessing(file, 3, 0);
 
-                // 获取InputStream
-                InputStream inputStream = new ByteArrayInputStream(byteArrayOut.toByteArray());
-                byteArrayOut.close();
 
                 List<SysUser> listSysUsers = ExcelImportUtil.importExcel(inputStream, SysUser.class, params);
 
@@ -2208,24 +2159,4 @@ public class SysUserController {
         // 如果没有找到匹配的节点,返回null
         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;
-        }
-    }
-
 }

+ 2 - 2
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/util/ExcelImportHandleUtil.java

@@ -19,9 +19,9 @@ public class ExcelImportHandleUtil {
      * @return inputStream (输入流)
      * @throws IOException
      */
-    public static InputStream fileStreamProcessing(MultipartFile file, int headerRowsNumber, int SheetIndex) throws IOException {
+    public static InputStream fileStreamProcessing(MultipartFile file, int headerRowsNumber, int sheetIndex) throws IOException {
         Workbook book = WorkbookFactory.create(file.getInputStream());
-        Sheet tempSheet = book.getSheetAt(SheetIndex);
+        Sheet tempSheet = book.getSheetAt(sheetIndex);
         int lastRowNum = tempSheet.getLastRowNum();
 
         // 创建一个新的Workbook和Sheet