Generating reports with JasperReports Sean C. Sullivan Portland Java Users Group April 2004
Agenda JasperReports JasperAssistant
JasperReports open source Java reporting tool
Using JasperReports 1.Create a JRXML file 2.Load the JRXML file 3.Compile the report 4.Fill the report with data 5.Export the report (PDF, HTML, etc)
Hello Jasper import dori.jasper.engine.*; import dori.jasper.engine.design.*; import java.sql.*; JasperDesign design = JasperManager.loadXmlDesign("hello.jrxml"); JasperReport report = JasperManager.compileReport(design);
Hello Jasper ResultSet rs = getResultSet(); JRDataSource dataSource = new JRResultSetDataSource(rs); Map params = new java.util.HashMap(); params.put("Bonus", new Integer(12000)); params.put("Year", “2004”);
Hello Jasper JasperPrint print = JasperManager.fillReport( report, param, dataSource); JasperExportManager.exportReportToHtmlFile( print, "hello.html"); JasperExportManager.exportReportToPdfFile( print, "hello.pdf"); JasperExportManager.exportReportToXmlFile( print, "hello.xml", false);
JasperReports 101 design objects –JasperDesign report objects –JasperReport datasource objects –JRDataSource print objects –JasperPrint
XML report templates jasperreport.dtd File extension: jrxml
Example: JRXML file
JasperDesign objects JasperManager.loadXMLDesign JasperManager.loadDesign
Manager classes dori.jasper.engine.JasperManager dori.jasper.engine.JasperCompileManager dori.jasper.engine.JasperFillManager dori.jasper.engine.JasperPrintManager dori.jasper.engine.JasperExportManager
Compiling designs JasperDesign design = JasperManager.loadXmlDesign("metrics.jrxml"); JasperReport report = JasperManager.compileReport(design);
Ant task: <jrc srcdir="${src.reports.dir}" destdir="${build.dir}/reports" tempdir="${build.dir}/temp" keepjava="true" xmlvalidation="true">
Data sources a data source is an object that provides data to Jasper's report engine do not confuse with javax.sql.DataSource
JRDataSource interface public interface JRDataSource { public boolean next() throws JRException; public Object getFieldValue(JRField field) throws JRException; }
Data source classes JRResultSetDataSource JRBeanArrayDataSource JRBeanCollectionDataSource JRMapArrayDataSource JRMapCollectionDataSource JRTableModelDataSource
Output formats CSV (JRCsvExporter) JPEG PDF (JRPdfExporter) XLS (JRXlsExporter) XML (JRXmlExporter) HTML (JRHtmlExporter)
JasperReports viewers Swing applications –dori.jasper.view.JasperViewer –dori.jasper.view.JasperDesignViewer
JasperViewer
JasperAssistant visual report designer commercial tool (not open source)
JasperAssistant JasperAssistant runs inside Eclipse
Demo: JasperAssistant
Open source projects
Commercial software ReportMill Actuate Windward Reports Crystal Reports Oracle Reports Cognos ReportNet Hyperion Reports
Beyond JasperReports Who is allowed to create and modify report templates? Where are the report templates stored? How will you publish and distribute the reports? Do you need to archive the reports? Will you generate reports on demand or on a fixed schedule (nightly, weekly) ? Is user A allowed to view report B?
Conclusion Download JasperReports today