ireport使用javabean作为数据源生成报表并导出 (pdf、excel、word)
1.首先需要下载报表模板制作软件,我使用的是iReport-5.6.0。下载好后开始画自己的模板。最好是先建一个工厂类,里面放一些静态数据,方便测试连接成功与否。然后点击工具菜单,选择选项,找到classpath,新建一个folder,路径就是tomcat下项目的classes(C:\apache-tomcat-6.0.35\webapps\Crt\WEB-INF\classes)。如图所示是工厂测试类:
然后配置数据源,在Connections/Datasources界面新建数据连接,类型选择JavaBeans set datasource,名字随便起,Factory class 填工厂测试类的路径(com.crt.alarmFactory),勾选collections of javabeans,static method 那一栏写getBeanCollection。然后点击test测试。测试成功后,在新的模板页面点击preview右侧的按钮(抱歉忘了叫啥了),选择javabean datasource,内容就是报表内容对应的实体类路径(com.crt.bean.tbLog)。加载出该类所有的属性。选择自己需要属性。在组建面板中选择,拖动Fields下自己需要的属性到页面中,更改对应的中文列名,调整好自己需要的样式。最后在说一下字体编码的设定:选中标签,字体选择宋体,pdf font name 选择STSong-Light,勾上pdf embedded,pdf encoding 选择UniGB-UCS2-H (Chinese Simplified)。至此,前端模板制作部分结束。
将制作好的模板(.jrxml文件)保存在自己定义好的文件夹下。下面直接贴出控制类中的代码:
if(dotype.equals("pdf")){
JasperDesign jasperDesign;
jasperDesign = JRXmlLoader.load("D:\\log.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRDataSource dataSource = new JRBeanCollectionDataSource(loglist);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
response.setHeader("Content-Disposition", "attachment;filename=temp.pdf");
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
else if(dotype.equals("excel")){
JasperDesign jasperDesign;
jasperDesign = JRXmlLoader.load("D:\\log.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRDataSource dataSource = new JRBeanCollectionDataSource(loglist);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
response.setContentType("application/vnd.ms-excel");
String defaultname="export.xls";
String fileName = new String(defaultname.getBytes("gbk"), "utf-8");
response.setHeader("Content-disposition", "attachment; filename="+ fileName);
ServletOutputStream ouputStream = response.getOutputStream();
JRXlsExporter exporter=new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS,Boolean.TRUE);
response.setHeader("Content-Disposition", "attachment;filename=first.xls");
response.setContentType("application/vnd_ms-excel");
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}
else if(dotype.equals("word")){
JasperDesign jasperDesign;
jasperDesign = JRXmlLoader.load("D:\\log.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRDataSource dataSource = new JRBeanCollectionDataSource(loglist);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
response.setContentType("application/vnd.ms-excel");
String defaultname="export.doc";
ServletOutputStream ouputStream = response.getOutputStream();
String fileName = new String(defaultname.getBytes("GBK"), "utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
JRAbstractExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}
这里我使用form表单方式请求,然后方法执行完后返回请求页面,页面下放就会弹出下载保存提示。如果页面中还使用ajax,为了不互相干扰,可以使用window.open打开一个小的页面,进行报表的导出。内容的在jsp页面的显示并没有使用ireport,主要是考虑到分页的实现不是很方便,而且样式也不如直接用html语言来的美观。另外是项目中需要的一些jar文件:groovy-all-1.7.5.jar,iTextAsian.jar,jasperreports-5.6.0.jar