JasperReports Ultimate Guide - Samples - Schema - Configuration - Functions - FAQ - API (Javadoc)
|
|
|
|
JasperReports - Batch Export Sample (version 6.21.0) | |
|
|
|
Main Features in This Sample | |
| Exporting Multiple Reports into a Single Output File (Batch Export) |
|
|
||||
top | |||||
|
|||||
Exporting Multiple Reports into a Single Output File (Batch Export) | Documented by Sanda Zaharia | ||||
|
|||||
| Description / Goal |
| Several reports can be exported together to form a single resulting document. | ||
| Since |
| 0.6.0 | ||
|
|||||
|
Exporter Input - API Overview
Once generated, a JasperPrint object may be exported to various output formats such as PDF, HTML, XML, CSV, RTF, Excel, MSWord, PPTX, etc. The JasperReports library includes a builtin exporter class for each output format enumerated here. An exporter should be able to handle a wide range of report sources, along with their specific export configuration settings. In order to perform the export, exporters require some specific input data:
Exporter builtin implementations acquire input data based on methods inherited from their JRAbstractExporter super class. When extending the JRAbstractExporter class, one can reuse the
As shown in the method signature, all we need is an ExporterInput object. This object must implement the
Another way to provide configuration settings globally is to use the overloaded
Builtin implementations Below is a list of the builtin implementations for the exporter input APIs:
The first thing an exporter needs to know is whether it is acting on a single JasperPrint report or on a list with several JasperPrint objects. Exporting multiple reports to a single resulting document is called batch mode exporting. Not all exporters can work in batch mode, but those that do first look into the exporter input data to see whether a list of several JasperPrint objects has been supplied to them. If so, the exporter loops through this list of reports and produces a single document from them. If the exporters act on a single report, then they check whether a single JasperPrint object is supplied to the exporter input data to be exported. As already shown, this object may be loaded from an input stream, an URL, a file object, or a file name. If no JasperPrint object is supplied, then the exporter throws an exception telling that no input source was set for the export process. Batch Mode Bookmarks When several JasperPrint documents must be concatenated in the same PDF file by batch export, you can introduce PDF bookmarks in the resulting PDF document to mark the beginning of each individual document that was part of the initial document list. These bookmarks have the same name as the original JasperPrint document as specified by the jasperPrint.getName() property. However, users can turn on and off
the creation of those bookmarks by using the following
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration(); configuration.setCreatingBatchModeBookmarks(true); //or false, if needed exporter.setConfiguration(configuration);export configuration setting. The exporter does not create such bookmarks by default. Code Samples This sample illustrates how to deal with exporter input settings when multiple reports are exported in batch mode. Looking into the src/BatchExportApp.java source file one can see that, first of all, 3 reports are filled to generate
JasperPrint objects:
public void fill() throws JRException { long start = System.currentTimeMillis(); //first JasperPrint object is generated and saved as .jrprint object in the build/reports directory JasperFillManager.fillReportToFile( "build/reports/Report1.jasper", null, new JREmptyDataSource(2) ); //second JasperPrint object is generated and saved as .jrprint object in the build/reports directory JasperFillManager.fillReportToFile( "build/reports/Report2.jasper", null, new JREmptyDataSource(2) ); //third JasperPrint object is generated and saved as .jrprint object in the build/reports directory JasperFillManager.fillReportToFile( "build/reports/Report3.jasper", null, new JREmptyDataSource(2) ); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); }Let's see now the batch export to the PDF output: public void pdf() throws JRException { long start = System.currentTimeMillis(); List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>(); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report1.jrprint")); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report2.jrprint")); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report3.jrprint")); JRPdfExporter exporter = new JRPdfExporter(); exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("build/reports/BatchExportReport.pdf")); SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration(); configuration.setCreatingBatchModeBookmarks(true); exporter.setConfiguration(configuration); exporter.exportReport(); System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); }One can see that a list of JasperPrint objects is loaded from previously created *.jrprint files. The list is used to populate the exporter input data, using the
SimpleExporterInput builtin implementation:
:
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
Based on the list of JasperPrint objects, the SimpleExporterInput will create and save a list of corresponding ExporterInputItem objects. Next, a global configuration setting is supplied to the exporter, using the SimplePdfExporterConfiguration implementation. This will instruct the exporter to take into account the batch export mode when creating bookmarks. SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration(); configuration.setCreatingBatchModeBookmarks(true); exporter.setConfiguration(configuration);Another example is the batch export to Excel 2007 (XLSX) output format: public void xlsx() throws JRException { long start = System.currentTimeMillis(); List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>(); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report1.jrprint")); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report2.jrprint")); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report3.jrprint")); JRXlsxExporter exporter = new JRXlsxExporter(); exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("build/reports/BatchExportReport.xlsx")); SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration(); configuration.setOnePagePerSheet(false); exporter.setConfiguration(configuration); exporter.exportReport(); System.err.println("XLSX creation time : " + (System.currentTimeMillis() - start)); }The main difference here is the global use of the SimpleXlsxReportConfiguration class, the builtin implementation of the ReportExportConfiguration interface fot the XLSX output format: SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration(); configuration.setOnePagePerSheet(false); exporter.setConfiguration(configuration);This configuration setting may differ from report to report, but when it's set on the exporter like above, it means that the same setting will be applied to al reports in the list. To have different settings per each report, we need to set a ReportExportConfiguration to each ExporterInputItem in the list. Running the Sample Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/batchexport within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/batchexport/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||
|
|
© 2001- Cloud Software Group, Inc. www.jaspersoft.com |