Excel列表數據導入到頁面列表

之前做產品清單遇到的。代碼如下:

@Autowired
FrameAgreementProListMapper mapper;

//導入報價產品
@RequestMapping("/importData.do")
@ResponseBody
public String importData(FrameAgreementProListModel model ,HttpServletRequest request ,HttpServletResponse response)throws Exception{
	//該model中只有  frameId 和file .前端傳的
	String filename = model.getFile().getOriginalFileName();
	String fileType = filename.substring(filename.lastIndexOf(".")); //====.xls
	List<String>  errorList = new LinkedList<>();//因爲返回String類型,所以這裏就用了一個errorList,裝錯誤信息返回。
	//獲取原產品清單列表
	model.setLoginName(SystemUtils.getUserModel().getLoginName());
  	List<FrameAgreementProListModel> oldList = mapper.selectFrameProList(model);
		if(fileType.equals(".xls")){
				List<FrameAgreementProListModel> importList = readExcel(model.getFile().getInputStream(), model.getFrameId(),errorList, mapper);
						if(errorList.isEmpty()){
								 	//讀取到Excel數據 | 未讀取到Excel數據
								 	if ( importList != null  ){
								 					//導入的Excel列表數據物料號 與 原列表數據物料號 一樣,則進行更新
								 					for( FrameAgreementProListModel  importModel : importList ){
															if( oldList != null ){ //原來有產品清單,進行物料號比對,選擇是更新還是添加
																	//我需要在這裏定義一個布爾變量,true則更新,false則新增
																	//我不能在內層進行更新操作或者添加,因爲那樣會拿第一個importModel依次比對oldModel進行操作
																	//然後第二個importModel,第三個。。。會有很多重複的。
																		boolean update = false;
																		for( FrameAgreementProListModel  oldModel : oldList ){
																				if(importModel.getPartNum() .equals(oldModel.getPartNum())){
																						importModel.setRowId(oldModel.getRowId());//設置Id
																						update = true;
																						break;
																				}
																		 }
																		 //在外層for循環進行 更新。操作。因爲我只關心importModel的 物料號是否與之前的一樣
																		 if(update){
																		 	service.update(importModel);
																		 }else{
																			service.addAndGetRowId(importModel);
																		 }														 
																}else{ //原來沒有產品清單,則直接添加。
																	super.asynEdit(importModel,request,response);
																	//我不能在這裏return,不然添加了一個就結束了
																	//需要在for循環之後,所有的數據都遍歷完,在return。							
																}																		
													}
								 		}else{
										errorList.add("無可導入的數據"); 
										return StringUtils.resultFailToJson(errorList);//說明沒有數據導入。
									  }
									//for循環外,只要沒有出錯,就必然會走到這裏來,所以在這裏return	
										return StringUtils.resultSuccessToJson("導入成功!");
							}
						return StringUtils.resultFailToJson(errorList);  //前面的代碼有問題,就返回錯誤信息
			}else{
			return StringUtils.resultFailToJson("您上傳的文件格式有誤,請上傳xls格式的文件!");	
			}
}

//讀取Excel內容,到model      -----mapper是我自己加的,因爲要用到。查數據
public static List<FrameAgreementProListModel> readExcel(InputStream is,String frameId, List<String>  errorList, FrameAgreementProListMapper mapper) throws Exception{
	//創建一個list 用來存儲讀取的內容
	List<FrameAgreementProListModel>  list = new LinkedList<FrameAgreementProListModel>();
	
	//獲取Excel 文件對象
try{	Workbook   rwb  =  Workbook.getWorkbook(is);

	//獲取文件的指定工作表 默認的第一個
	Sheet  sheet = rwb.getSheet(0);
	Cell  cell1  = ExcelUtils.getCellByValue(sheet,"物料號");
	Cell  cell2  = ExcelUtils.getCellByValue(sheet,"銷售價格");
	Cell  cell3  = ExcelUtils.getCellByValue(sheet,"實際質保期");
	Cell  cell4  = ExcelUtils.getCellByValue(sheet,"服務費(百分比)");

	if(cell1 == null || cell2 == null || cell3 == null || cell4 == null ){
			errorList.add("模板格式不正確");
			return list;
	}

	int  int_i  =  cell1.getRow() + 1;
	for(int  i = ini_i ; ; i++){
		 try{		StringBuffer error = new StringBuffer();

				//物料號
				Cell val1 = sheet.getCell(cell1.getColumn(),i);
				if(StringUtils.isBlank(val1.getContents())){
					break;
				}
		
				//銷售價格
				Cell val2 = sheet.getCell(cell2.getColumn(),i);
				if(!"".equals(val2.getContents())){
					Integer num = Integer.parseInt(val2.getContents());
					if(  num < 0 ) error.append("[銷售價格]"必須爲正數)    ;
				}else{
					error.append("[銷售價格]必填");
				}

			//實際質保期
				Cell val3 = sheet.getCell(cell3.getColumn(),i);
				if(!"".equals(val3.getContents())){
					//判斷當前數字是否爲正數0-9
					try{
							Double.parseDoubel(val3.getContents());
						}catch(Exception e){ //如果不是正數,就不能解析成Double?
							if(error.length() > 0)  error.append(",");
							error.append("[實際質保期]必須爲正數");
						}
				}else{
					error.append("[實際質保期]必填");
				}

				//服務費百分比
				Cell val4 = sheet.getCell(cell4.getColumn(),i);
				if(!"".equals(val4.getContents())){
					//判斷當前數字是否爲正數0-9
					try{
							Double.parseDoubel(val4.getContents());
						}catch(Exception e){ //如果不是正數,就不能解析成Double?
							if(error.length() > 0)  error.append(",");
							error.append("[服務費百分比]必須爲正數");
						}
				}else{
					error.append("[服務費百分比]必填");
				}
	
				if( error.length() > 0 ){
						errorList.add(String.format("行號【%d】:%s",i+1,error.toString()));
					}
				
			//讀取到Model中
			FrameAgreementProListModel model = new FrameAgreementProListModel();
				model.setPartNum( val1.getContents() ); //物料號
				model.setSalesPrice(val2.getContents()); //銷售價格
				model.setActualWP(val3.getContents()); //實際質保期
				model.setServiceCost(val4.getContents()); //服務費百分比
				model.setFrameId(frameId); //關聯框架。
				//根據物料號,查詢產品,拿到產品的RowId。設置到當前Model的產品IdProductId.
				List<FrameAgreementProListModel > proList = mapper.selectProList(model);
				model.setProductId(SystemUtils.getUserModel().getRowId());
				list.add(model);	
		}catch(Exception e){
			e.printStackTrace();
			continue;
		}
		
	}//for循環
 		return list;
		}finally{ //第一個try
			rwb.close();
		}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章