本人菜鸟,这篇文章纯属我的记录,主要功能是经过java和Ireport生成报表,并生成pdf,word,excel格式java
java代码中赋值到IReport里面动态生成报表app
//建立报表字段源,必须是List集合 //mainReportData 是一个bean,设定:里面的每个字段对应子报表的数据源 JRDataSource dataSource= new JRBeanCollectionDataSource(Arrays.asList(mainReportData)); //parameters 添加的参数,(这里的参数和fields能够互换,在Ireport中设置就能够了) Map parameters = new HashMap(); parameters.put("SUBREPORT_DIR", this.getClass().getResource("/").getPath() + "com/gx/jasperReport/monthReport/"); parameters.put("LOGO_PATH", this.getClass().getResource("/").getPath() + "com/gx/jasperReport/monthReport/logo.png"); parameters.put("month", reportMonth.split("-")[1]); JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName, parameters, dataSource);
其实也就是能够传两个东西, 一个是参数(hashMap),一个是字段集合(List(DataBean))this
直接在左侧面板-Paramerters-添加Paramerte,名字是上面的hashMap的key,类型对应。.net
直接在左侧面板-Fields-添加Field,名字是上面的dataSource中的的dataBean的成员,类型对应。 excel
到此,主报表设置完成。code
建立子报表很简单,直接在组件面板中拖到主视图中,选择Create a new report.下一步,到数据源时选择Empty dataSource, 而后下一步下一步...Don't use any conection ...完成。blog
以下图,参数对应子报表参数, 数据源对应数据源教程
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Arrays.asList($F{currentMonthIssue}))
以下:提供三种下载格式:pdf, word,xcel(最好的是用pdf ,word会格式问题)图片
switch (exportType){ case "pdf": JRPdfExporter exporter = new JRPdfExporter(); //jasperreport6.3 //exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); //jasperreport5.0 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); response.setHeader("Content-disposition", "attachment;filename=" + reportMonth + "_" + System.currentTimeMillis() + ".pdf"); response.setContentType("application/pdf"); response.setCharacterEncoding("UTF-8"); //jasperreport6.3 //exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream())); //jasperreport5.0 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); exporter.exportReport(); break; case "xls": JRXlsExporter xlsExporter = new JRXlsExporter(); xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); response.setHeader("Content-disposition", "attachment;filename=" + reportMonth + "_" + System.currentTimeMillis() + ".xls"); response.setContentType("application/xls"); response.setCharacterEncoding("UTF-8"); xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); xlsExporter.exportReport(); break; case "word": JRExporter wordExporter = new JRRtfExporter(); wordExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); response.setHeader("Content-disposition", "attachment;filename=" + reportMonth + "_" + System.currentTimeMillis() + ".doc"); response.setContentType("application/doc"); response.setCharacterEncoding("UTF-8"); wordExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); wordExporter.exportReport(); }