1. 程式人生 > >Jxls匯出excel的若干方式總結(十三)-- 動態合併單元格

Jxls匯出excel的若干方式總結(十三)-- 動態合併單元格

可以看到程式碼中指定合併的部分現在已經顯示出了效果。

模板

List supplyAreaList = saBiz.getSupplyAreaById(supplyAreaId);
			SupplyArea sa = (SupplyArea) supplyAreaList.get(0);
			Long id = sa.getSupplyAreaId();
			List qcList = new ArrayList();
			QueryCondition idObj = new QueryCondition();
			idObj.setFieldName("supplyAreaId");
			idObj.setQueryOperator(QueryOperator.le);
			idObj.setValue(id);
			qcList.add(idObj);
			List recordsList = saBiz.getRecords(qcList, new SupplyArea());
			int i = recordsList.size();
			String templateDir = "D:/excel/template/SupplyAreaDynaMergeCells.xls";
			String targetDir="D:/excel/export/testDynaMergeCells.xls";		
			InputStream is = new FileInputStream(templateDir);
			Map beans = new HashMap();
			beans.put("suplyArea", recordsList);
			//關聯模板 
			XLSTransformer transformer = new XLSTransformer();
			HSSFWorkbook workBook = transformer.transformXLS(is, beans);
			HSSFSheet sheet = workBook.getSheetAt(0);
			sheet.addMergedRegion(new Region(2,(short)0,2+i,(short)0));
			sheet.addMergedRegion(new Region(2,(short)2,2+i,(short)2));
			OutputStream os = new FileOutputStream(targetDir);
	       	workBook.write(os);
	        	is.close();
	        	os.flush();
os.close();


匯出結果