lyhzzz 2 tháng trước cách đây
mục cha
commit
32ad9f7466

+ 11 - 3
src/main/java/com/fdkankan/agent/service/impl/ExcelServiceImpl.java

@@ -23,10 +23,9 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
 import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 
 /**
  * <p>
@@ -135,12 +134,21 @@ public class ExcelServiceImpl implements IExcelService {
             throw new BusinessException(ResultCode.FILE_TYPE_ERROR);
         }
         List<HashMap<Integer, String>> excelRowList = new ArrayList<>();
+        List<HashMap<Integer, String>> excelRowListTemplate = new ArrayList<>();
         try {
             excelRowList = ExcelUtil.getExcelRowList(file);
+            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/authModel.xlsx");
+            excelRowListTemplate = ExcelUtil.getExcelRowList(inputStream);
         }catch (Exception e){
             log.info("uploadExcel-error:{}",e);
             throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
         }
+        HashMap<Integer, String> mapt = excelRowListTemplate.get(0);
+        HashMap<Integer, String> map1 = excelRowList.get(0);
+        if(!mapt.equals(map1)){
+            throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
+        }
+
         List<Integer> errorIndex = new ArrayList<>();
         List<AuthModelingParam> params = new ArrayList();
         Integer index = 0;

+ 44 - 1
src/main/java/com/fdkankan/agent/util/ExcelUtil.java

@@ -56,6 +56,10 @@ public class ExcelUtil {
         return rowList;
     }
 
+    private static Workbook getExcelWorkBook(InputStream inputStream) throws IOException {
+        return new XSSFWorkbook(inputStream);
+    }
+
   private static  String fommartNum(String value){
         try {
             if(isNumeric2(value) && value.contains(".")){
@@ -72,7 +76,46 @@ public class ExcelUtil {
     }
 
 
-    //获取WorkBook对象
+
+    public static List<HashMap<Integer,String>> getExcelRowList(InputStream inputStream) throws IOException {
+        //行List,也是最终要返回的List
+        List<HashMap<Integer,String>> rowList=new ArrayList<>();
+        Workbook workbook=getExcelWorkBook(inputStream);
+        Sheet sheet = workbook.getSheetAt(0);
+        if (sheet == null) {
+            throw new IOException("创建Sheet失败!");
+        }
+        //开始遍历行
+        for (int i=0;i<= sheet.getLastRowNum();i++){
+            Row row = sheet.getRow(i);
+            //列List
+            HashMap<Integer,String> map = new HashMap<>();
+            if(row == null){
+                rowList.add(map);
+                continue;
+            }
+            //转换为List数组
+            for (int cellNum=0;cellNum<= row.getLastCellNum();cellNum++){
+                Cell cell = row.getCell(cellNum);
+                if (cell != null && cell.getCellTypeEnum() != CellType.STRING && HSSFDateUtil.isCellDateFormatted(cell))
+                {
+                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+                    Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
+                    String value = sdf.format(date);
+                    map.put(cellNum,value);
+                    continue;
+                }
+                if(cell != null){
+                    String value = fommartNum(cell.toString().trim());
+                    map.put(cellNum,value);
+                }
+
+            }
+            rowList.add(map);
+        }
+        return rowList;
+    }
+
     private static Workbook getExcelWorkBook(MultipartFile multipartFile) throws IOException {
         InputStream inputStream=multipartFile.getInputStream();
         String originalFileName=multipartFile.getOriginalFilename();