现在的位置: 首页 > 综合 > 正文

Jxls导出excel的若干方式总结(十三)– 动态合并单元格

2017年10月26日 ⁄ 综合 ⁄ 共 1010字 ⁄ 字号 评论关闭

可以看到代码中指定合并的部分现在已经显示出了效果。

模板

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();

导出结果

抱歉!评论已关闭.