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();