JasperReports
A huge proportion of programming activity involves the production of reports. Almost all programming environments provide simple means of writing text to a file or a priniter. Laying out a good lookiing report is a tedious process. To alleviate the drudgery and speed productivity, a lot of attention has been paid to report generators.
The most common proprietary method is Crystal Reports. The most comprehensive "free" alternative is JasperReports. The native environment for JasperReports is Java. Typically, one would print a report in response to a menu selection. This example uses the ActionPerformed event associated with a menu item.
private void casesTLVMReportMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_casesTLVMReportMenuItemActionPerformed
String reportSource = "./report/templates/cases.jrxml";
String reportDest = "./report/results/Cases.html";
Map<String, Object> params = new HashMap<String, Object>();
try
JasperReport jasperReport =
JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport, params, new JRTableModelDataSource(casesTable.getModel()));
JasperExportManager.exportReportToHtmlFile(
jasperPrint, reportDest);
JasperViewer.viewReport(jasperPrint,false);
}
catch (JRException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_casesTLVMReportMenuItemActionPerformedNotes:
- JasperReports must be integrated into Netbeans
- the Table and the Menu are generated in Netbeans Matisse
- reportSouce is a file generated by iReport
- reportDest is an HTML output version
- compileReport compiles the reportSource
- fillReport takes data from a Matisse generated JTable
- exportReportToHtmlFile write the report in HTML format to reportDest
- viewReport pops up the report in the JasperViewer
