2) 一般,建立与数据库的连接,并设置报表查询语句
3) 在设计器中,设置动态信息(例如:字段的绑定、变量、参数等)及分组、排序、小计等
4) 保存(.jrxml)并编译成.jsper文件
5) 以Web方式(HTM, JSP, Servlet)访问报表
三、Jasperreport及ireport的特点
1)可以生成常用格式的报表,如HTML、PDF、XLS等
2)可以根据传递的参数动态生成报表
3)HTML格式的报表可以分页显示
4)报表模板、SQL语句、程序分离
5)报表界面美观,并且不可编辑
四、编译JRXML
一般,可以先使用iReport设计报表模板(.jrxml),并编译为.jasper文件。
五、在JSP或SERVLET中访问报表
代码参考,如下所示
代码(略)
String reportFileName = application.getRealPath("/reports/myFirstJasperreport.jasper");
File reportFile = new File(reportFileName);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reportFileName,
null,
DatabaseConn.getConnection()
);
六、在JSP或SERVLET中生成报表
代码(略)
1)生成HTML报表
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "servlets/image?image=");
exporter.exportReport();
2)生成PDF报表
response.setContentType("application/pdf");
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
OutputStream ouputStream = response.getOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.exportReport();
3)生成EXCEL报表
File destFile = new File(reportFile.getParent(), jasperPrint.getName() + ".xls");
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.exportReport();