java servlet导出EXCEL并合并EXCEL相同值的单元格(Apache POI技术)

@ResponseBody
	@RequestMapping(params = "method=loadOutExcel")
	public void loadOutExcel(HttpServletRequest request) throws IOException{
		
		//人员登录id
		Farmer farmer = (Farmer) request.getSession().getAttribute("farmer");
		int id = farmer.getId();
	//根据登录id,从数据库读取全部值	
	List<OrderWaitPojo> orderDetailsList = orderDetailService.getHistoryOrderDetailListExcel(id);
		
		
		XSSFWorkbook workbook = new XSSFWorkbook(); 
		XSSFCellStyle cellStyle = workbook.createCellStyle();   
		//创建EXCEL表名
		XSSFSheet spreadsheet = workbook.createSheet("历史订单");
		XSSFRow row=spreadsheet.createRow(0);
		XSSFCell cell;
		//在EXCEL的第一行建立各列的标题
		cell=row.createCell(0);
		cell.setCellValue("配送时间");
		cell=row.createCell(1);
	    cell.setCellValue("订单量");
	    cell=row.createCell(2);
	    cell.setCellValue("农场名称");
	    cell=row.createCell(3);
	    cell.setCellValue("商品名称");
	    cell=row.createCell(4);
	    cell.setCellValue("商品数量");
	    cell=row.createCell(5);
	    cell.setCellValue("单位");
	    
	    int countIndex = 0;
	    String deli_day = ""; //定义一个临时存储值
	    int startRow = 1;  
	    
	    int sum=0;
	    int countIndex_s = 0;
	    int startRow_s = 1; 
	    
	    int i=1;
	    for(OrderWaitPojo o:orderDetailsList){
	    	row=spreadsheet.createRow(i);
	    	cell=row.createCell(0);
	    	cell.setCellValue(o.getDelivery_day());
	    	
	    	if("".equals(deli_day)){//当第一次循环的时候将第一个实际值赋值给临时变量
	    		deli_day=o.getDelivery_day();
		    }else{
		    	if(o.getDelivery_day().equals(deli_day)){ //第二次循环取得的值与第一次循环取得值                                                                进行对比,如果相等,计数器countIndex+1
		    	                                         
		    		countIndex ++;
		    	}else{
		    	        //如果比较不相等,则合并单元格,现在合并的是从第startRow行开始,到startRow                                +countIndex行结束,后边两个0是从第0列开始,到第0列结束
		    		spreadsheet.addMergedRegion(new CellRangeAddress(startRow,startRow+countIndex, 0, 0));
		    		startRow = startRow+countIndex+1;  
		    		countIndex = 0;  
		    		deli_day=o.getDelivery_day();
		    	}
		    }
	    	
	    	
	    	cell=row.createCell(1);
	    	cell.setCellValue(o.getSum());
	    	
	    	if(sum==0){
	    		sum = o.getSum();
	    	}else{
	    		if(o.getSum()==sum){
	    			countIndex_s ++;
	    		}else{
	    			spreadsheet.addMergedRegion(new CellRangeAddress(startRow_s,startRow_s+countIndex_s, 1, 1));
		    		startRow_s = startRow_s+countIndex_s+1;  
		    		countIndex_s = 0;  
		    		sum=o.getSum();
	    		}
	    	}
	    	
	    	cell=row.createCell(2);
	    	cell.setCellValue(o.getFarm_name());
	    	cell=row.createCell(3);
	    	cell.setCellValue(o.getProduct_name());
	    	cell=row.createCell(4);
	    	cell.setCellValue(o.getAmount());
	    	cell=row.createCell(5);
	    	cell.setCellValue(o.getUnit());
	    	i++;
	    }
	    FileOutputStream out = new FileOutputStream(new File("D:/历史订单.xlsx"));
	    workbook.write(out);
	    out.close();
	    System.out.println(
	    	      "exceldatabase.xlsx written successfully");
	}


你可能感兴趣的:(java,POI技术))