Selaa lähdekoodia

导入用户工时研究开发

anderx 1 vuosi sitten
vanhempi
commit
6ac4370c6a

+ 12 - 8
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java

@@ -1,21 +1,17 @@
 package com.ruoyi.web.controller.system;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.lang3.ArrayUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.core.controller.BaseController;
@@ -288,4 +284,12 @@ public class SysUserController extends BaseController
     public AjaxResult selectName(String name, Integer type){
         return success(userService.selectName(name,type));
     }
+
+    @PostMapping("/importUserClock")
+    public AjaxResult importUserClock( @RequestParam("file") MultipartFile file) throws Exception{
+        ExcelUtil<Map> util = new ExcelUtil<>(Map.class);
+        List<Map<Integer, String>> maps = util.importMapExcel(file.getInputStream(), 2);
+        maps.stream().forEach(System.out::println);
+        return success("");
+    }
 }

+ 84 - 3
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java

@@ -307,6 +307,11 @@ public class ExcelUtil<T>
         return list;
     }
 
+    public List<T> importExcel(InputStream is, int titleNum) throws Exception
+    {
+        return importExcel(StringUtils.EMPTY, is, titleNum);
+    }
+
     /**
      * 对excel表单默认第一个索引名转换成list
      *
@@ -314,9 +319,85 @@ public class ExcelUtil<T>
      * @param titleNum 标题占用行数
      * @return 转换后集合
      */
-    public List<T> importExcel(InputStream is, int titleNum) throws Exception
-    {
-        return importExcel(StringUtils.EMPTY, is, titleNum);
+    public List<Map<Integer,String >> importMapExcel(InputStream is, int titleNum) throws Exception {
+        List<Map<Integer,String >> list =null;
+        try {
+            list =importMapExcel(StringUtils.EMPTY, is, titleNum);
+        } catch (Exception e)
+        {
+            log.error("导入Excel异常{}", e.getMessage());
+            throw new UtilException(e.getMessage());
+        }
+        finally
+        {
+            IOUtils.closeQuietly(is);
+        }
+        return list;
+
+    }
+
+
+    public List<Map<Integer,String >> importMapExcel(String sheetName, InputStream is, int titleNum) throws Exception{
+        this.type = Type.IMPORT;
+        this.wb = WorkbookFactory.create(is);
+        List<Map<Integer,String >> list = new ArrayList<>();
+        // 如果指定sheet名,则取指定sheet中的内容 否则默认指向第1个sheet
+        Sheet sheet = StringUtils.isNotEmpty(sheetName) ? wb.getSheet(sheetName) : wb.getSheetAt(0);
+        if (sheet == null)
+        {
+            throw new IOException("文件sheet不存在");
+        }
+        boolean isXSSFWorkbook = !(wb instanceof HSSFWorkbook);
+        Map<String, PictureData> pictures;
+        if (isXSSFWorkbook)
+        {
+            pictures = getSheetPictures07((XSSFSheet) sheet, (XSSFWorkbook) wb);
+        }
+        else
+        {
+            pictures = getSheetPictures03((HSSFSheet) sheet, (HSSFWorkbook) wb);
+        }
+        // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
+        int rows = sheet.getLastRowNum();
+        if (rows > 0)
+        {
+            // 定义一个map用于存放excel列的序号和field.
+            Map<Integer,String > cellMap = new HashMap<>();
+            // 获取表头
+            Row heard = sheet.getRow(titleNum);
+            for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
+            {
+                Cell cell = heard.getCell(i);
+                if (StringUtils.isNotNull(cell))
+                {
+                    String value = this.getCellValue(heard, i).toString();
+                    cellMap.put( i,value);
+                }
+                else
+                {
+                    cellMap.put( i,null);
+                }
+            }
+            list.add(cellMap);
+            for (int i = titleNum + 1; i <= rows; i++){
+                Map<Integer,String> map = new HashMap<>();
+                // 从第2行开始取数据,默认第一行是表头.
+                Row row = sheet.getRow(i);
+                // 判断当前行是否是空行
+                if (isRowEmpty(row)){
+                    continue;
+                }
+                for (int x=0; x<=row.getLastCellNum(); x++){
+                    String val=null;
+                    if (row.getCell(x) !=null){
+                         val= row.getCell(x).getStringCellValue();
+                    }
+                    map.put( x,val);
+                }
+                list.add(map);
+            }
+        }
+        return list;
     }
 
     /**