PoiExcel导入实现(解决跨行问题以及03和07版本问题)

Excel数据导入

需求:需要将多个跨行的数据保存在数据库中,而且数据库中的数据是树形结构,多行多列存在父级关系,数据需要保存父ID。

解决方案: 因为是树形结构,那么表单的数据分区读取,先读取父级的数据存到数据库,再存入子类数据。

第一步:解决Excel版本问题

String originalFileName = file.getOriginalFilename();
    		InputStream is = file.getInputStream();
    		int version = 0;
    		if (originalFileName.endsWith(".xls")) {
    			version = 2003;
    		}else if (originalFileName.endsWith(".xlsx")) {
    			version = 2007;
    		}else {
    			throw new Exception("Incorrect file format,Only allowed '.xls,.xlsx' extension");
    		}
    		Workbook workbook = null;
    		switch (version) {
    			case 2003:
    				POIFSFileSystem fs = new POIFSFileSystem(new BufferedInputStream(is));
    				workbook = new HSSFWorkbook(fs);
    				break;
    	
    			case 2007:
    				workbook = new XSSFWorkbook(new BufferedInputStream(is));
    				break;
    		}
    		int sheetIndex = workbook.getSheetIndex("云运维成熟度评估要素");
    		Sheet sheet = workbook.getSheetAt(sheetIndex);
		

  

第二步:遍历第二、三列数据存入list便于下一步处理

 List<OcScope> ocScopeListOne = new ArrayList<OcScope>();
String b = "";  String c = ""; 	 int e =0; double w = 0D;
    		for (Row row : sheet) {
    			OcScope ocScope = new OcScope();
    			int rowNum = row.getRowNum();
    			if(e==1){break;}  //当e==1时跳出循环
    			if(rowNum <= 1){//跳出第一行   一般第一行都是表头没有数据意义
    				continue;
    			}
    			int startCell = 1; //
    			if(row.getCell(startCell)!=null ){//第1列数据
  		          row.getCell(startCell).setCellType(Cell.CELL_TYPE_STRING);
  		          if(!row.getCell(startCell).getStringCellValue().equals("")){  
                        //判空赋值的好处是解决跨行数据 如果下一行没有读到就继续将上一行的数据写入实体
  		        	  
  		        	  if(row.getCell(startCell).getStringCellValue().equals("0")){ //如果数据为0跳出循环
  		        		  e=1;   
  		        	  }else{
  		        		 b = row.getCell(startCell).getStringCellValue();  //不是就赋值
  		        	  }
  		        	
  		          }
  		         
    			}
    			startCell++;
    			if(row.getCell(startCell) == null){
		        	  e=1;
		          }
    			
    			if(row.getCell(startCell)!=null){//第15列
    		    	row.getCell(startCell).setCellType(Cell.CELL_TYPE_NUMERIC);
    		    		 w = row.getCell(startCell).getNumericCellValue();
    		    		     }
    			//解决跨行问题
    			ocScope.setScopeName(b); //只有不为空的时候b才会被从新赋值
    			ocScope.setParentName(null);
    			ocScope.setLevel("1");
    			ocScope.setWeights(w);//只有不为空的时候W才会被从新赋值
    			ocScopeListOne.add(ocScope);
    		}

  

第三步:去重处理(因为处理的是对象,用Map去重是一个不错的解决方案)

Map<String, OcScope> ocScopeMapOne = Maps.newHashMap();
    		Map<String, OcScope>  ocScopeMapTwo = Maps.newHashMap();
    		for (OcScope ocScope : ocScopeListOne) {
    			ocScopeMapOne.put(ocScope.getScopeName(), ocScope);
			}

  

第四步:存入处理(遍历Map存入)

OcScope ocScopeOne = new OcScope();
    		ocScopeOne.setLevel("1");
        	List<OcScope> ocScopelistOne=	ocScopeService.findList(ocScopeOne);  //查询出一级的所有数据
    		for (String key : ocScopeMapOne.keySet()) { //遍历map
    			for (OcScope ocScope1 : ocScopelistOne) {
    				if(!ocScopeMapOne.get(key).getScopeName().equals(ocScope1.getScopeName())){ //与一级的list数据进行对比没有相同的就存入新数据
//    					dao.insert(ocScopeMapOne.get(key));
				}
				}
    		}

  

第五步:从新遍历表格存入下一级数据, 遍历第二、四、五列数据存入list便于下一步处理

b=""; c="";w=0; e =0;  //重新赋值
    		for (Row row : sheet) {
    			OcScope ocScope = new OcScope();
    			int rowNum = row.getRowNum();
    			if(e==1){break;}
    			if(rowNum <= 1){//跳出第一行   一般第一行都是表头没有数据意义
    				continue;
    			}
    			int startCell = 1; //
    			if(row.getCell(startCell)!=null ){//第1列数据
  		          row.getCell(startCell).setCellType(Cell.CELL_TYPE_STRING);
  		          if(!row.getCell(startCell).getStringCellValue().equals("")){
  		        	 b = row.getCell(startCell).getStringCellValue();
  		          }
  		         
    			}
    			
    			startCell++;
    			startCell++;
    			if(row.getCell(startCell) == null){
		        	  e=1;
		          }
    			if(row.getCell(startCell)!=null){//第3列数据
    		          row.getCell(startCell).setCellType(Cell.CELL_TYPE_STRING);
    		          if(!row.getCell(startCell).getStringCellValue().equals("")){
    		        	  if(row.getCell(startCell).getStringCellValue().equals("0")){
    		        		  e=1;  
    		        	  }else{
    		        		  c = row.getCell(startCell).getStringCellValue();
    		        	  }
    	  		        	
    	  		          }
    		     }
    			    			
    			startCell++;
    			if(row.getCell(startCell)!=null){//第4列
    		    	row.getCell(startCell).setCellType(Cell.CELL_TYPE_NUMERIC);
    		    	if(row.getCell(startCell).getNumericCellValue()!=0.0){
    		    		w = row.getCell(startCell).getNumericCellValue();
    		    	}
    		    		 
    		    	
    		    	}
    			
    		
//    		 转换为Integer类型
    			//解决跨行问题
    			
    			ocScope.setParentName(b); //这里需要存一个父级的名子
    			ocScope.setScopeName(c);
    			ocScope.setLevel("2");
    			ocScope.setWeights(w);
    			ocScopeListTwo.add(ocScope);
    		}

  

第六步:去重处理+存入处理

for (OcScope ocScope : ocScopeListTwo) { //去重处理
    			ocScopeMapTwo.put(ocScope.getScopeName(), ocScope);
			}


	 OcScope ocScopeTwo = new OcScope();
    		 ocScopeTwo.setLevel("2");
    	List<OcScope> ocScopelistTwo=	ocScopeService.findList(ocScopeTwo); //取出二级的List数据
        for (String key : ocScopeMapTwo.keySet()) {   //遍历map
    			for (OcScope ocScope1 :  ocScopelistOne) {
    				if(ocScopeMapTwo.get(key).getParentName().equals(ocScope1.getScopeName())){  //给对象赋值父ID根据值判断
    					ocScopeMapTwo.get(key).setParentId(ocScope1.getId());
        			}
				}
    		}
    		System.err.println("ocScopeMapTwo+Pid"+ocScopeMapTwo);
    		for (String key : ocScopeMapTwo.keySet()) {  //遍历map
    			for (OcScope ocScope1 : ocScopelistTwo) {  
    				if(!ocScopeMapTwo.get(key).getScopeName().equals(ocScope1.getScopeName())){//数据库没有相关数据就存入进去
//    					dao.insert(ocScopeMapOne.get(key));
				}
				}
    				
    		}

  

猜你喜欢

转载自www.cnblogs.com/jiajialeps/p/10182645.html
今日推荐