Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,74 MB
Nội dung
Chapter 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 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark 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 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark Chapter 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 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 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark 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 This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark 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 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark Chapter 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 import import import import net.sf.jasperreports.engine.JRException; net.sf.jasperreports.engine.JRExporterParameter; net.sf.jasperreports.engine.JasperPrint; net.sf.jasperreports.engine.export.JRPdfExporter; 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 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark 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 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark Chapter [ 247 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark 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 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge 4310 Eon Conway www.verypdf.com Dr NW, , Atlanta, ,to 30327 remove this watermark ... 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... watermark 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...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;