lyhzzz il y a 1 an
Parent
commit
8c713ed6be

+ 1 - 0
src/main/java/com/fdkankan/manage/service/impl/ExcelServiceImpl.java

@@ -92,6 +92,7 @@ public class ExcelServiceImpl implements IExcelService {
         try {
             excelRowList = ExcelUtil.getExcelRowList(file);
         }catch (Exception e){
+            log.info("updateExcel-error:{}",e);
             throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
         }
         switch (type){

+ 52 - 6
src/main/java/com/fdkankan/manage/util/ExcelUtil.java

@@ -1,14 +1,14 @@
 package com.fdkankan.manage.util;
 
 import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -34,7 +34,7 @@ public class ExcelUtil {
             //转换为List数组
             for (int cellNum=0;cellNum<= row.getLastCellNum();cellNum++){
                 Cell cell = row.getCell(cellNum);
-                if (cell != null && ObjectUtils.isNotEmpty(cell.getStringCellValue()) && cell.getCellTypeEnum() != CellType.STRING && HSSFDateUtil.isCellDateFormatted(cell))
+                if (cell != null && cell.getCellTypeEnum() != CellType.STRING && HSSFDateUtil.isCellDateFormatted(cell))
                 {
                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                     Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
@@ -46,7 +46,6 @@ public class ExcelUtil {
                     DataFormatter dataFormatter = new DataFormatter();
                     String cellValue = dataFormatter.formatCellValue(cell);
                     map.put(cellNum,cellValue);
-
                 }
 
             }
@@ -55,7 +54,7 @@ public class ExcelUtil {
         return rowList;
     }
 
-  private static  String fommartNum(String value){
+    private static  String fommartNum(String value){
         try {
             if(isNumeric2(value) && value.contains(".")){
                 return Double.valueOf(value).intValue() +"";
@@ -64,12 +63,45 @@ public class ExcelUtil {
             return value;
         }
         return value;
-  }
+    }
 
     public static boolean isNumeric2(String str) {
         return str != null && str.matches("-?\\d+(\\.\\d+)?");
     }
 
+    public static List<HashMap<Integer,String>> getExcelRowList(File multipartFile) throws IOException {
+        //行List,也是最终要返回的List
+        List<HashMap<Integer,String>> rowList=new ArrayList<>();
+        Workbook workbook=getExcelWorkBook(multipartFile);
+        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<>();
+            //转换为List数组
+            for (int cellNum=0;cellNum<= row.getLastCellNum();cellNum++){
+                Cell cell = row.getCell(cellNum);
+                if(ObjectUtils.isNotEmpty(cell)){
+                    DataFormatter dataFormatter = new DataFormatter();
+                    String cellValue = dataFormatter.formatCellValue(cell);
+                    System.out.println(cellValue);
+                    map.put(cellNum,cellValue);
+                }
+
+            }
+            rowList.add(map);
+        }
+        return rowList;
+    }
+
+    public static void main(String[] args) throws IOException {
+        getExcelRowList(new File("D:\\abc\\111111\\rtk账号导入模板 (1).xlsx"));
+    }
+
 
     //获取WorkBook对象
     private static Workbook getExcelWorkBook(MultipartFile multipartFile) throws IOException {
@@ -85,4 +117,18 @@ public class ExcelUtil {
             return new XSSFWorkbook(inputStream);
         }
     }
+
+    private static Workbook getExcelWorkBook(File multipartFile) throws IOException {
+        InputStream inputStream = new FileInputStream(multipartFile);
+        String originalFileName=multipartFile.getName();
+        assert originalFileName != null;
+        String fileType=originalFileName.substring(originalFileName.lastIndexOf(".")+1);
+        if (fileType.equalsIgnoreCase("xls")) {
+            //xls格式
+            return new HSSFWorkbook(inputStream);
+        } else {
+            //xlsx格式
+            return new XSSFWorkbook(inputStream);
+        }
+    }
 }

BIN
src/main/resources/template/cameraOut.xlsx


BIN
src/main/resources/template/rtkAccount.xlsx


BIN
src/main/resources/template/rtkAccountFailureTime.xlsx