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

Jxls导出excel的若干方式总结(十四)– 动态设置分页符

2017年12月03日 ⁄ 综合 ⁄ 共 1232字 ⁄ 字号 评论关闭

因为在导出excel表格的时候,存在一条记录占用多行,并且一次导出若干条记录。为了用户打印方便,所以要针对性对这些记录进行分页设置。以下示例,模板设计时一条记录占用7行,实测6条记录占用一页,故代码中分页以此为依据进行了分页。

模板

分页代码:
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());
			String templateDir = "D:/excel/template/SupplyAreaChangeRow.xls";
			String targetDir="D:/excel/export/testChangeRow.xls";			
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(templateDir));//传入模板文件
			//InputStream bis = new BufferedInputStream(new FileInputStream(templateDir));//传入模板文件
			Map beans = new HashMap();
			beans.put("suplyArea", recordsList);
			XLSTransformer transFormer = new XLSTransformer();
			HSSFWorkbook workBook = transFormer.transformXLS(bis,beans);
			OutputStream os = new FileOutputStream(targetDir);
			HSSFSheet sheet = workBook.getSheetAt(0);
			for(int i=1;i<recordsList.size()/7+2;i++ ){
				//sheet.setAutobreaks(true);//经测试证明所有记录为一页,即不分页
				sheet.setRowBreak(i*6*7-1);//每页显示6条记录,每条记录占7行,共j页	
				sheet.setColumnBreak((short)5);
			}
			workBook.write(os);
			os.flush();
			os.close();

导出结果

打印预览

抱歉!评论已关闭.