本文主要是介绍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查询出审核时间、审核结果、申请时间、以及考生IDExamPrintModel 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);//组织数据源JRBeanCollectionDataSourceCollection 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的更加高级的应用还有待深入了解。我只是简单用到这个技术实现打印功能而已。以后用到有机会也要好好研究研究。
自己学习的话网上有很多教程。
这篇关于iReport实现报表的打印功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!