iReport实现报表的打印功能

    用iReport开发报表打印功能需要下载此软件,这是开发报表模型的一款软件,需要用到的一些jar包名字以及开发用到资料,自行网上可以下载学习

然后在里面建立自己的模型,例如我新建的一个新的模型。需要注意的是$F{examineeName}等等参数要和实体字段名字相同,否则数据传递不过来而且会报错。

在Action方法建立自己的方法:也可以自己建立简单的实现方法。

@RequestMapping(value = "/exportReceiptModel.action", method = RequestMethod.GET)
	public String exportReceiptModel(HttpServletRequest request,HttpServletResponse response, ModelMap model,String applyId){
		CmsSite site = CmsUtils.getSite(request);
		FrontUtils.frontData(request, model, site);
		String sol = site.getSolutionPath();
	    //根据申请号码ID查询出审核时间、审核结果、申请时间、以及考生ID
		ExamPrintModel examModel = new ExamPrintModel();
		examModel.setApplyid(applyId);
		examModel =printFrontService.getExportReceiptModelinfo(examModel);
		
		printFrontService.printExportReceipt(request, response, examModel, "export");
		
		return FrontUtils.getTplPath(request, sol, Constants.TPLDIR_PRINT, PRINT_DONE);
	}
	
在server实现方法,这个方法主要是获取数据并且准备数据为传递数据到模板文件做好准备的

/**
	 * 获取打印回执需要的具体数据
	 * @param examPrintModel
	 * @return
	 * modify by fts 2014.04.29
	 */
	public ExamPrintModel getExportReceiptModelinfo(ExamPrintModel examPrintModel){
		ReissueInfo reissueinfo=examineeInfoDaoImpl.find(ReissueInfo.class,Long.parseLong(examPrintModel.getApplyid()));
		ExamineeInfo examineeInfo = examineeInfoDaoImpl.find(ExamineeInfo.class,reissueinfo.getExamineeID());
		examPrintModel.setCheckDateTime(TimeUtil.changeStrTimeFormat(reissueinfo.getCheckDateTime().substring(0,8),"yyyyMMdd", "yyyy年MM月dd日"));      //审核时间
		examPrintModel.setResult(reissueinfo.getResult().equals("P")?"通过":"不通过");                   //审核结果
		examPrintModel.setCreateDateTime(TimeUtil.changeStrTimeFormat(reissueinfo.getCreateDateTime().substring(0,8),"yyyyMMdd", "yyyy年MM月dd日"));   //申请时间
		if(examineeInfo.getExamineeID()!=""){
			examPrintModel.setMobile(examineeInfo.getMobile());                  //电话号码
			examPrintModel.setExamineeName(examineeInfo.getExamineeName());      //姓名
			examPrintModel.setSex((examineeInfo.getSex()).equals("1")?"男":"女"); //性别
			examPrintModel.setCredentiano(examineeInfo.getCredentiaNO());        //身份证号码
			examPrintModel.setUnitName(examineeInfo.getUnitName());              //单位名称
		}
		return examPrintModel;
	}

最后打印的代码,这是打印的实现代码

/**
	 * 打印补办申请回执
	 * @param request
	 * @param response
	 * @param trainPrintModel
	 * @param flag
	 */
	@SuppressWarnings({ "unchecked", "deprecation","unused" })
	public void printExportReceipt(HttpServletRequest request, HttpServletResponse response,ExamPrintModel examPrintModel,String flag) {
		try{
			//获取报表模板
			File reportFile = new File(request.getRealPath("r/cms/www/red/print/zs_bbClass.jasper"));
			JasperReport jasper = (JasperReport)JRLoader.loadObject(reportFile);
			//组织数据源JRBeanCollectionDataSource
			Collection c=new ArrayList();
			c.add(examPrintModel);
			JRDataSource jrDs = new JRBeanCollectionDataSource(c);
			Map param = new HashMap();
			//填充报表
			byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), param, jrDs);
			// 生成PDF文件
			response.setContentType("application/pdf");
			//判断是否是下载
			if("export".equals(flag)){
				String fileName = new String("补办申请回执".getBytes("GB2312"),"ISO_8859_1");
				response.setHeader("Content-disposition","attachment; filename="+fileName+".pdf");
			}
			ServletOutputStream ouputStream = response.getOutputStream();
		    ouputStream.write(bytes,0,bytes.length);
		    ouputStream.flush();
		    ouputStream.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

简单的打印出的结果是:

对于iReport的更加高级的应用还有待深入了解。我只是简单用到这个技术实现打印功能而已。以后用到有机会也要好好研究研究。

自己学习的话网上有很多教程。

最近在学习IReport4.5方面的知识,但是在网上有很多的资料,但是资料的说明都是3.X版本或者是更早版本的。 在学习的过程中遇到很多的问题,在这里不一一列出了,现在这个小实例就是关于iReport自带的打印功能,版本是4.5。网上资料都是3.0的打印,但是我们用4.5做的报表模板用3.0的库去打印难免会出现问题,比如说4.5中有Table组件而3.0版本中没有,如果使用3.0的库去打印必然是出现问题的。为解决这一问题精心设计了这个小程序。 程序很灵活实用,为解决大家没有数据库的情况,本应用是针对于JavaBean做为数据源进行传递参数。 下面有我说一下怎么使用: 首先在print.jsp中可以修改jrprint的VALUE的值来确定程序调用的那个一.jasper文件,这里不是写死的,大家可以灵活的应用 <PARAM NAME = "jrprint" VALUE ="report_javaBeans.jasper"> 我门在print.jsp中已经设置好参数以后接下来就是AppletServlet.java中去执行就可以了 String jrprint = request.getParameter("jrprint"); 获得传递jrprint参数 File reportFile = new File(this.getServletContext().getRealPath("/"+jrprint)); JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),null,dataSource);//执行报表程序 程序中都很明了 大家肯定会问print.jsp怎么会调用到AppletServlet.java中去执行呢 其实很简单主要是靠这句话 <PARAM NAME = CODE VALUE = "ViewerApplet.class" > ViewerApplet.class其实已经封装好了,大家无需修改可以直接用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值