Parcourir la source

修改三级页面Excel转json时出现的错位bug

xujiawei il y a 5 ans
Parent
commit
fcd7ac2542
1 fichiers modifiés avec 29 ajouts et 8 suppressions
  1. 29 8
      src/main/java/edu/nju/util/ExcelToJson.java

+ 29 - 8
src/main/java/edu/nju/util/ExcelToJson.java

@@ -22,10 +22,14 @@ public class ExcelToJson {
 			int xssfLastRowNum = xssfSheet.getLastRowNum();
 			JSONObject object = new JSONObject();
 			JSONObject second = new JSONObject();
+			//是否是第一行
+			boolean firstRow=true;
+			//是否更改一级页面
+			boolean changeFirstPage=false;
 			for(int rowNum = 1; rowNum <= xssfLastRowNum; rowNum++) {
 				XSSFRow xssfRow = xssfSheet.getRow(rowNum);
 				if(xssfRow == null) { continue; }
-				
+
 				XSSFCell cell = xssfRow.getCell(0);
 				String cellValue0 = cell.getStringCellValue();
 				if(cellValue0 != "") {
@@ -35,16 +39,29 @@ public class ExcelToJson {
 					}
 					object.put("item", cellValue0);
 					object.put("children", new JSONArray());
+					changeFirstPage=true;
+				}else{
+					changeFirstPage=false;
 				}
-				
+
 				cell = xssfRow.getCell(1);
 				String cellValue1 = cell.getStringCellValue();
 				if(cellValue1 != "") {
 					if(second.length() != 0) {
-						JSONArray array = (JSONArray)object.get("children");
-						array.put(second);
-						object.put("children", array);
-						second = new JSONObject();
+						//不是第一行且更改了第一级界面,需要将上一个一级页面的最后一个二级页面放进去
+						if(changeFirstPage&&!firstRow){
+							JSONArray array = (JSONArray) dataArray.getJSONObject(dataArray.length()-1).get("children");
+							array.put(second);
+							second = new JSONObject();
+							changeFirstPage=false;
+							firstRow=false;
+						}else{
+							//直接将二级页面放入当前一级页面
+							JSONArray array = (JSONArray) object.get("children");
+							array.put(second);
+							object.put("children", array);
+							second = new JSONObject();
+						}
 					}
 					second.put("item", cellValue1);
 					second.put("children", new JSONArray());
@@ -56,10 +73,14 @@ public class ExcelToJson {
 				third.put("item", cell.getStringCellValue());
 				arr.put(third);
 				second.put("children", arr);
-				
+				firstRow=false;
 			}
+
 			dataArray.put(object);
-			
+			//最后一个一级页面的最后一个二级页面也要放入
+			JSONArray array = (JSONArray) dataArray.getJSONObject(dataArray.length()-1).get("children");
+			array.put(second);
+
 			return dataArray;
 		} catch (Exception e) {
 			e.printStackTrace();