leon 4 лет назад
Родитель
Сommit
8868ce09bf
1 измененных файлов с 45 добавлено и 11 удалено
  1. 45 11
      src/main/java/com/mokamrp/privates/help/Analysis.java

+ 45 - 11
src/main/java/com/mokamrp/privates/help/Analysis.java

@@ -3,10 +3,8 @@ package com.mokamrp.privates.help;
 import com.mokamrp.privates.entity.VoltaHandle;
 import com.mokamrp.privates.utils.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.CellType;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -60,6 +58,13 @@ public class Analysis {
             for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                 //循环获取工作表的每一行
                 Row sheetRow = sheet.getRow(i);
+                if (sheetRow == null){
+                    continue;
+                }
+                //.如果每行首列为空 直接视为本行没数据
+                if(Analysis.getCellValue(sheetRow.getCell(0)).isEmpty()){
+                    continue;
+                }
                 //循环获取每一列
                 Map<String,String> cell = new HashMap<>();
                 for (int j = 0; j < sheetRow.getPhysicalNumberOfCells(); j++) {
@@ -73,14 +78,11 @@ public class Analysis {
                     if (param.length > 1){
                         paramRule = param[1];
                     }
-                    if (sheetRow.getCell(j) == null){
-                        continue;
-                    }
-                    sheetRow.getCell(j).setCellType(CellType.STRING);
+                    String cellStr = Analysis.getCellValue(sheetRow.getCell(j));
                     //如果列数据为空,检查是否允许此列数据为空
-                    if (sheetRow.getCell(j).getStringCellValue() == null || sheetRow.getCell(j).getStringCellValue().isEmpty()) {
+                    if (cellStr.isEmpty()) {
                         if (paramRule == null){
-                            String collName = sheet.getRow(0).getCell(j).getStringCellValue();
+                            String collName = Analysis.getCellValue(sheet.getRow(0).getCell(j));
                             res.setErr("第"+i+"行,("+collName+")列数据不可为空,请检查表格");
                             return res;
                         }else if (paramRule.equals("default")) {
@@ -89,7 +91,7 @@ public class Analysis {
                             paramValue = paramRule;
                         }
                     }else{
-                        paramValue = sheetRow.getCell(j).getStringCellValue();
+                        paramValue = cellStr;
                     }
                     if(j>=field.size()){
                         break;
@@ -127,4 +129,36 @@ public class Analysis {
         }
 
     }
+
+    public static String getCellValue(Cell cell) {
+        String value = "";
+        if (cell != null) {
+            // 以下是判断数据的类型
+            switch (cell.getCellTypeEnum()) {
+                case NUMERIC: // 数字
+                    cell.setCellType(CellType.STRING);
+                    value = cell.getStringCellValue() + "";
+                    break;
+                case STRING: // 字符串
+                    value = cell.getStringCellValue();
+                    break;
+                case BOOLEAN: // Boolean
+                    value = cell.getBooleanCellValue() + "";
+                    break;
+                case FORMULA: // 公式
+                    value = cell.getCellFormula() + "";
+                    break;
+                case BLANK: // 空值
+                    value = "";
+                    break;
+                case ERROR: // 故障
+                    value = "非法字符";
+                    break;
+                default:
+                    value = "";
+                    break;
+            }
+        }
+        return value.trim();
+    }
 }