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");
	}


转载于:https://my.oschina.net/projerry/blog/474096

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值