JasperReports 3.5 for Java Developers- P6

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

0
48
lượt xem
12
download

JasperReports 3.5 for Java Developers- P6

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'jasperreports 3.5 for java developers- p6', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: JasperReports 3.5 for Java Developers- P6

  1. Chapter 8 Clicking on the nodes will direct the main window to the appropriate anchor. Handling very large reports Sometimes, when filling a report, the report datasource may have a lot of data. In some cases, the generated report can become very large, and in some cases larger than the memory allocated for the JVM, causing an OutOfMemoryException. It is possible to set up JasperReports so that it stores segments of a report on the disk in order to free some memory. This can be accomplished by using a built-in report parameter REPORT_VIRTUALIZER. The value for this parameter must be an instance of a class implementing net.sf.jasperreports.engine.JRVirtualizer. JasperReports comes with an implementation of this interface, namely net.sf. jasperreports.engine.fill.JRFileVirtualizer. This implementation is sufficient to handle the vast majority of the large reports. If, for some reason, this implementation is not sufficient for our needs, we can always create our own implementation of net.sf.jasperreports.engine.JRVirtualizer. The following example illustrates typical usage of JRVirtualizer: package net.ensode.jasperbook; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; [ 239 ]
  2. Other JasperReports Features import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.fill.JRFileVirtualizer; public class DbConnectionReportFill { Connection connection; public void generateReport(String reportName) { String reportDirectory = "reports"; JRFileVirtualizer fileVirtualizer = new JRFileVirtualizer(3, "cacheDir"); HashMap parameterMap = new HashMap(); parameterMap.put(JRParameter.REPORT_VIRTUALIZER, fileVirtualizer); try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/flightstats?" + "user=user&password=secret"); System.out.println("Filling report..."); JasperFillManager.fillReportToFile(reportDirectory + "/" + reportName + ".jasper", parameterMap,connection); System.out.println("Done!"); connection.close(); } catch (JRException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { new DbConnectionReportFill().generateReport(args[0]); } } [ 240 ]
  3. Chapter 8 The JRFileVirtualizer class has two constructors. The one we chose to use in the example takes two parameters. The first parameter is the maximum number of report pages that will be stored in primary memory (RAM) before the sections of the report are stored in virtual memory (disk). The second parameter is the directory that will be used to store the segments of the report that will be stored on disk. The other constructor takes a single parameter, an int indicating the maximum number of report pages that will be stored in primary memory. When using this constructor, the cached portions of the report will be stored in the working directory of the running application. We need to do nothing special in the JRXML template to be able to cache them to disk. The process described in this section makes filling a report a much slower process than usual. Therefore, report virtualization should be used only when there is a good possibility that the report will cause the JVM to run out of memory. Summary In this chapter, we discussed several features that allow us to create elaborate reports. We learned to render localized reports by using the resourceBundle attribute of the JRXML element. We then used scriptlets to add complex functionality to our reports, including variable value modification and performance measurement. We saw how to add cross-tabulation tables (crosstabs) to our reports by taking advantage of the JRXML element and display related charts or crosstabs for each record in a report by using subdatasets. To ease the task of report navigation, we learned how to add hyperlinks, anchors, and bookmarks to our reports. We have also seen how we can safely generate reports larger than the available memory by taking advantage of report virtualization. [ 241 ]
  4. Exporting to Other Formats Reports can be exported to several formats. Because reports in native JasperReports format can be viewed only by using the JasperReports API (or by using the JasperViewer utility included with JasperReports), exporting reports is a common requirement. Exported reports can be viewed with readily available software like PDF viewers, word processors, and web browsers. In this chapter, we will learn how to export our reports to all of the formats supported by JasperReports. Topics covered in this chapter include: • Exporting reports to PDF • Exporting reports to RTF • Exporting reports to ODT • Exporting reports to Excel • Exporting reports to HTML • Exporting reports to XML • Exporting reports to CSV • Exporting reports to plain text • Directing exported reports to a browser
  5. Exporting to Other Formats Exporting overview Exporting reports is done using a series of classes that implement the net.sf.jasperreports.engine.JRExporter interface. This interface contains, among others, the following two methods: • public void setParameter(JRExporterParameter parameter, java. lang.Object value) • public void exportReport() The setParameter() method is used to set the parameters needed to export the report. In most cases, two parameters need to be set: the name of the output file or output stream used to output the exported report and the JasperPrint object containing the native report. We would set the output file any time we are sure we want to save the exported report to the disk. We would set the output stream parameter to send the exported report through the network or when we are not sure if we want to save the exported report to the disk or stream it through the network. As an output stream can be easily saved to the disk or streamed through the network, the decision can be made at the runtime. As can be seen in the signature of the setParameter() method, it takes an instance of net.sf.jasperreports.engine.JRExporterParameter as its first argument. JRExporterParameter contains a number of static constants that are typically used as the first argument to the setParameter() method. To accommodate the most common cases, the JRExporterParameter constants of interest are: • JRExporterParameter.JASPER_PRINT: This is used to set the JasperPrint object to export. • JRExporterParameter.OUTPUT_FILE_NAME: This is used to set the output filename. • JRExporterParameter.OUTPUT_STREAM: This is used to set the output stream. There are several other constants defined in JRExporterParameter. Consult the JavaDoc documentation for JRExporterParameter at http://jasperreports.sourceforge.net/api/net/sf/ jasperreports/engine/JRExporterParameter.html for details. As we will see in the following sections, exporting to different formats follows the same pattern in all cases. Once we are familiar with the procedure to export to one format, learning to export to other formats will be trivial. [ 244 ]
  6. Chapter 9 Exporting reports functionality is done entirely in Java code; the JRXML does not need to be modified at all. For most of the examples in this chapter, we will be using the subdatasets example from the previous chapter. Before moving on, it is worth mentioning that for most formats, exported reports keep their formatting (fonts, colors, and so on). The only two formats that lose their formatting are CSV and plain text because both of these are plain text files containing no formatting information. Exporting to PDF We have already seen the examples of exporting reports to PDF in previous chapters. However, all the examples we have seen so far stream a PDF report straight to the browser window. In the following example, we will export a report to PDF and save it to the filesystem: package net.ensode.jasperbook; import java.io.File; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; public class PdfExportDemo { public static final String REPORT_DIRECTORY = "reports"; public void pdfExport(String reportName) { File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint"); try { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file); JRPdfExporter pdfExporter = new JRPdfExporter(); pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, REPORT_DIRECTORY + "/" + reportName + ".pdf"); System.out.println("Exporting report..."); pdfExporter.exportReport(); System.out.println("Done!"); } [ 245 ]
  7. Exporting to Other Formats catch (JRException e) { e.printStackTrace(); } } public static void main(String[] args) { new PdfExportDemo().pdfExport(args[0]); } } As we can see in the example, the JRExporter implementation used to export to PDF is net.sf.jasperreports.engine.export.JRPdfExporter. We need to pass it to the compiled report in the native JasperReports format by setting the JRExporterParameter.JASPER_PRINT parameter to the appropriate instance of net.sf.jasperreports.engine.JasperPrint. Because we are saving the report to disk, we set the output filename to be the report name. The only difference is that we substitute the file extension with "pdf". The code we just wrote will generate a PDF that looks like the following screenshot: [ 246 ]
  8. Chapter 9 [ 247 ]
  9. Exporting to Other Formats Exporting to RTF Rich Text Format (RTF) is a document file format that is supported by most word processors. Exporting to RTF allows our documents to be read by Microsoft Word and several other word processors. Unfortunately, RTF documents generated by JasperReports are not always readable by OpenOffice.org or StarOffice writer because these office suites are not fully compliant with the RTF specification. As we'll see in the next section, JasperReports can export to OpenDocument Text, the native format for both of these office suites. The following example illustrates how to export a report into RTF format: package net.ensode.jasperbook; import java.io.File; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRRtfExporter; import net.sf.jasperreports.engine.util.JRLoader; public class RtfExportDemo { public static final String REPORT_DIRECTORY = "reports"; public void rtfExport(String reportName) { File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint"); try { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file); JRRtfExporter rtfExporter = new JRRtfExporter(); rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); rtfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, REPORT_DIRECTORY + "/" + reportName + ".rtf"); System.out.println("Exporting report..."); rtfExporter.exportReport(); System.out.println("Done!"); } catch (JRException e) { e.printStackTrace(); } } [ 248 ]
  10. Chapter 9 public static void main(String[] args) { new RtfExportDemo().rtfExport(args[0]); } } As we can see in this example, net.sf.jasperreports.engine.export. JRRtfExporter is the JRExporter implementation we need to use to export to RTF. Like the previous example, we tell the exporter what report to export by supplying an instance of net.sf.jasperreports.engine.JasperPrint as the value for the JRExporterParameter.JASPER_PRINT parameter, and we set the output file to be the report name by setting the JRExporterParameter.OUTPUT_FILE_NAME with the appropriate value. This example code will generate an RTF document as shown in the following screenshot: [ 249 ]
  11. Exporting to Other Formats Exporting to ODT OpenDocument Text (ODT) is the word processing standard for Organization for the Advancement of Structured Information Standards (OASIS) and the native format of several open source word processing tools, most notably OpenOffice.org Writer. Reports can be exported to ODT by taking advantage of the JROdtExporter class provided with JasperReports. The following example illustrates how to do this: package net.ensode.jasperbook; import java.io.File; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.oasis.JROdtExporter; import net.sf.jasperreports.engine.util.JRLoader; public class OdtExportDemo { public static final String REPORT_DIRECTORY = "reports"; public void odtExport(String reportName) { File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint"); try { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file); JROdtExporter odtExporter = new JROdtExporter(); odtExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); odtExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, REPORT_DIRECTORY + "/" + reportName + ".ods"); System.out.println("Exporting report..."); odtExporter.exportReport(); System.out.println("Done!"); } catch (JRException e) { e.printStackTrace(); } } public static void main(String[] args) { new OdtExportDemo().odtExport(args[0]); } } [ 250 ]
  12. Chapter 9 As we can see, exporting to ODT is not much different from exporting to other formats. The JRExporter implementation that we need to use in this case is net.sf.jasperreports.engine.export.oasis.JROdtExporter. Note that in the previous examples, we have specified what report to export by supplying an instance of net.sf.jasperreports.engine.JasperPrint as the value for the JRExporterParameter.JASPER_PRINT parameter. We then set the output file to be the report name by setting JRExporterParameter.OUTPUT_FILE_NAME with the appropriate value. The following screenshot illustrates how the BarChartReportDemo example from Chapter 7, Adding Charts and Graphics to Reports, is rendered in OpenOffice.org Writer after being exported to ODT: [ 251 ]
  13. Exporting to Other Formats Exporting to Excel It is not uncommon to request reports in Microsoft Excel format as Excel allows easy manipulation of report data to perform calculations. JasperReports provides built-in capability to export reports to Excel. The following example demonstrates this functionality: package net.ensode.jasperbook; import java.io.File; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JExcelApiExporter; import net.sf.jasperreports.engine.util.JRLoader; public class XlsExportDemo { public static final String REPORT_DIRECTORY = "reports"; public void xlsExport(String reportName) { File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint"); try { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file); JExcelApiExporter xlsExporter = new JExcelApiExporter(); xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, REPORT_DIRECTORY + "/" + reportName + ".xls"); System.out.println("Exporting report..."); xlsExporter.exportReport(); System.out.println("Done!"); } catch (JRException e) { e.printStackTrace(); } } public static void main(String[] args) { new XlsExportDemo().xlsExport(args[0]); } } [ 252 ]
  14. Chapter 9 This example follows the same pattern as the previous examples in this chapter. The JRExporter implementation needed to export to Excel is net.sf.jasperreports. engine.export.JExcelApiExporter. Again, we set the report to export and the output filename by setting the appropriate parameters on JExcelApiExporter. This example will generate an Excel spreadsheet that looks like the following screenshot: JasperReports includes two Excel exporters: JExcelApiExporter and JRXlsExporter. It is preferable to use JExcelApiExporter because JRXlsExporter does not support exporting images. JExcelApiExporter is the newer Excel exporter. JRXlsExporter is still included for backward compatibility. [ 253 ]
  15. Exporting to Other Formats Exporting to HTML Exporting to HTML is another common requirement. The following example demonstrates how to do it: package net.ensode.jasperbook; import java.io.File; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRHtmlExporter; import net.sf.jasperreports.engine.util.JRLoader; public class HtmlExportDemo { public static final String REPORT_DIRECTORY = "reports"; public void htmlExport(String reportName) { File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint"); try { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file); JRHtmlExporter htmlExporter = new JRHtmlExporter(); htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); htmlExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, REPORT_DIRECTORY + "/" + reportName + ".html"); System.out.println("Exporting report..."); htmlExporter.exportReport(); System.out.println("Done!"); } catch (JRException e) { e.printStackTrace(); } } public static void main(String[] args) { new HtmlExportDemo().htmlExport(args[0]); } } In this example, we generate an HTML file and save it to disk. The JRExporter implementation for HTML export is net.sf.jasperreports.engine.export. JRHtmlExporter. Like in the previous examples, we set the report to export and the filename by setting the appropriate parameters. [ 254 ]
  16. Chapter 9 A common requirement when exporting to HTML is to have the exported report directed to a browser window. This technique will be covered in the last section in this chapter. The code in the example will generate an HTML report that looks like the following: Reports exported to HTML result in a single HTML file, regardless of how many pages the original report has. [ 255 ]
  17. Exporting to Other Formats Exporting to XML JasperReports uses a Document Type Definition (DTD) file to generate XML reports. XML reports can be exported back to the compiled reports by using the net.sf.jasperreports.engine.xml.JRPrintXmlLoader class. The following example demonstrates how to export a report to XML: package net.ensode.jasperbook; import java.io.File; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRXmlExporter; import net.sf.jasperreports.engine.util.JRLoader; public class XmlExportDemo { public static final String REPORT_DIRECTORY = "reports"; public void xmlExport(String reportName) { File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint"); try { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file); JRXmlExporter xmlExporter = new JRXmlExporter(); xmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); xmlExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, REPORT_DIRECTORY + "/" + reportName + ".jrpxml"); System.out.println("Exporting report..."); xmlExporter.exportReport(); System.out.println("Done!"); } catch (JRException e) { e.printStackTrace(); } } public static void main(String[] args) { new XmlExportDemo().xmlExport(args[0]); } } [ 256 ]
  18. Chapter 9 As we can see in the example, the JRExporter implementation used to export to XML is net.sf.jasperreports.engine.export.JRXmlExporter. The same procedure used in the previous examples is used to set the report to export and to the resulting filename. Notice that the filename used for the exported report contains the extension jrpxml. Even though exported reports are standard XML files, it is customary to use this extension instead of xml. The following is a partial listing of the generated XML file: [ 257 ]
  19. Exporting to Other Formats The DTD for the XML generated when exporting to XML can be found at http://jasperreports.sourceforge.net/dtds/jasperprint.dtd. Reports exported to XML can be viewed with the JasperViewer utility included with JasperReports. To view a report exported to XML, the XML argument needs to be passed to it. For example, to view this XML report, the following command needs to be typed in the command line (assuming all required libraries are already in the CLASSPATH): net.sf.jasperreports.view.JasperViewer -Freports/DatasetDemoReport.jrpxml -XML Exporting reports to XML has some advantages over using the compiled report directly. For example, exported reports are human readable and editable, and they can easily be stored in a database without resorting to Binary Large Objects (BLOBS). Exporting to CSV Comma Separated Values (CSV) files contain a number of values separated by commas. There are several software utilities that can parse CSV files. JasperReports includes built-in functionality to export reports to CSV files. The following example illustrates the process: package net.ensode.jasperbook; import java.io.File; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRCsvExporter; import net.sf.jasperreports.engine.util.JRLoader; public class CsvExportDemo { public static final String REPORT_DIRECTORY = "reports"; public void csvExport(String reportName) { File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint"); try { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file); JRCsvExporter csvExporter = new JRCsvExporter(); [ 258 ]
Đồng bộ tài khoản