天天看點

POI 動态合并單元格

所謂動态合并,要明白,怎樣才能合并,

是根據id還是根據其他的,

比如根據id,要想合并,那麼要合并的兩項的資料必須挨着,

那麼問題就在于sql 怎麼去寫.

是以,POI動态合并的操作就一句代碼,主要還是在sql, order by XXX 

/ *****
查詢結果 
****** /
	/ **
	 *報表導出
	 *: 
	 * @throws異常
	 * /
	public void queryExcelActionInfo(OutputStream out,
			Map <String,Object>參數)throws Exception {
		List <ActionCount> queryActionInfo = actionCountMapper
				.queryExcelReportInfo(parameters);


		/ ****************************************** POI ****** ********************************************* /
		System.out
				.println(“============================================== =================“);
		HSSFWorkbook book = new HSSFWorkbook();
		HSSFSheet sheet = book.createSheet(“口腔saas操作統計”);
		for(int i = 0; i <18; i ++){
			sheet.setColumnWidth(i,4000);


		}}
		/ *****************************設定表頭***************** ****************** /
		HSSFRow row1 = sheet.createRow(0);

		//此處可以 循環建立cell
		HSSFCell cell1 = row1.createCell(0),cell2 = row1.createCell(1),cell3 = row1
				.createCell(2),cell4 = row1.createCell(3),cell5 = row1
				.createCell(4);
			cell1.setCellValue(“序号”);
			cell2.setCellValue(“名稱”);
			cell3.setCellValue(“時間”);
			cell4.setCellValue(“區域(位址)”);		
			
		/ ***********************循環建立表格*********************** *************** /
		int count = 0; //合并序号
		for(int i = 0; i <queryActionInfo.size(); i ++){
			HSSFRow row = sheet.createRow((int)i + 1);
			ActionCount actionCount = queryActionInfo.get(i);

			//建立單元格并設定值
		
			if(null!= actionCount.getOrgName()){
				row.createCell(1).setCellValue(actionCount.getOrgName());
			}}
			if(null!= actionCount.getInstalltime()){
				String string = DateUtil.StringToString(actionCount.getInstalltime(),“yyyy-MM-dd”);
				row.createCell(2).setCellValue(string);
			}}
			if(null!= actionCount.getAddress()){

				row.createCell(3).setCellValue(actionCount.getAddress());
			}}
			if(null!= actionCount.getUsername()){

				row.createCell(4).setCellValue(actionCount.getUsername());
			}}
			
			
			if(i!= 0){
				//比較id如果一樣則合并
				if(queryActionInfo.get(i-1).getOrgID().equals(queryActionInfo.get(i).getOrgID())){
					/ * sheet.addMergedRegion(new CellRangeAddress(1,1,0,0)); 
					
					  參數一:起始行,參數二:結束行,參數三:起始列,參數四:結束列
				 	  此處可以循環進行合并		
					* /
				sheet.addMergedRegion(new CellRangeAddress(i,1 + i,0,0));
				sheet.addMergedRegion(new CellRangeAddress(i,1 + i,1,1));
				sheet.addMergedRegion(new CellRangeAddress(i,1 + i,2,2));
				sheet.addMergedRegion(new CellRangeAddress(i,1 + i,3,3));   
				count ++;
					}} 
				}}
			row.createCell(0).setCellValue(Integer.parseInt(actionCount.getRowNo() -  count));
		}}
		book.write(out);
				
}}
           

//循環合并:

if (i != 0) {
	if (purchaseLibList.get(i - 1).getPurId().equals(purchaseLibList.get(i).getPurId())) {
            // 參數一:起始行,參數二:結束行,參數三:起始列,參數四:結束列
	    for (int j = 0; j < 12; j++) {
	          sheet.addMergedRegion(new CellRangeAddress(i, 1 + i, j, j));
		}

	}
   }
           
//Controller  導出 相容性
		if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
			fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// firefox浏覽器
		} else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
			fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏覽器
		} else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
			fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌
		}
           
poi

繼續閱讀