Youve learned so far how the CrystalReportViewer object can be used either in code or as a tag library to view reports in HTML format. This is useful for having a quick look at a report online, but users often require the capability to save the report to their own machine either for their own reference or so they can send the report elsewhere. Exporting is a perfect solution to this. The Java Reporting Component supports exporting reports to both Adobe PDF and RTF. There are two ways exporting can be done: via the export button on the toolbar and via code.
By default, the export button on the report viewers toolbar is hidden. To enable it, either set the displayToolbarExportButton attribute to true if you are using the tag library or call the setHasExportButton method if you are using the viewer directly.
Even though you instruct the viewer to show the export button, you might find that it is still now showing up. This is most likely because you have not told the viewer that it owns the whole page. This is done via the setOwnPage method or isOwnPage attribute for the viewer or tag library, respectively.
When the Export button is clicked, a pop-up window appears asking the user which document format she would like to export the report to and which pages she would like to export to. This is shown in Figure 28.3.
When the user clicks OK, the browser sends back the report in the requested format. Figure 28.4 shows the Income Statement report from the previous examples, exported to PDF.
There are a few reasons why you might want to export via code. Perhaps you always want to deliver reports in PDF or RTF format instead of using the report viewer at all. Or perhaps you want to control the user interface for exporting. In any case, this section describes how to export using the ReportExportControl.
Although some developers find it attractive to bypass the Crystal Report HTML viewer, and instead use either PDF or RTF as the primary way to deliver reports, this is often not the best way to go. Exporting is one of most processor-intensive operations and thus should be used sparingly if possible. In addition, when you export reports you lose all the interactive functionality like drill-down and group tree navigation. Use exporting where appropriate.
The ReportExportControl is the Java object used to render reports to both PDF and RTF. Because it is derived from the same class as the CrystalReportViewer object, it has many of the same properties and methods. The two main methods used in the report viewersetReportSource and processHttpRequestare used in exactly the same way in the ReportExportControl. Also, when exporting there is an additional method that is required: setExportOptions. This method is used to tell the ReportExportControl which export format should be used, and optionally, which pages should be exported.
The argument that is passed into the setExportOptions method is an object of type ExportOptions. This is found in com.crystaldecisions.sdk.occa.report.exportoptions package. With it, you call the setExportFormatType method passing in one of the following values:
Although the ReportExportFormat object has additional export formats not mentioned here such as MSExcel and Text, these are not currently available with the Java Reporting Component. These show up because the ExportOptions object is a shared object between other Crystal products that do support those export format types.
Listing 28.3 pulls this all together and shows a JSP page that exports a report to PDF format.
<%@ page contentType="text/html;charset=UTF-8" import="com.crystaldecisions.reports.reportengineinterface.*, com.crystaldecisions.report.web.viewer.*, om.crystaldecisions.sdk.occa.report.exportoptions.*" %> <% // name of report file String reportFile = "Income_Statement.rpt"; // create the JPEReportSourceFactory JPEReportSourceFactory rptSrcFactory = new JPEReportSourceFactory(); // call the createReportSource method Object reportSource = rptSrcFactory.createReportSource(reportFile, request.getLocale()); // create the report viewer ReportExportControl exporter = new ReportExportControl(); // set the report source exporter.setReportSource(reportSource); ExportOptions exportOptions = new ExportOptions(); exportOptions.setExportFormatType(ReportExportFormat.PDF); exporter.setExportOptions(exportOptions); // tell the viewer to display the report exporter.processHttpRequest(request, response, getServletConfig().getServletContext(), null); %>
There are a few additional options that you might find useful. The first is the capability to specify which page numbers should be exported. This enables you to export just a small number of pages from a very large report. This is accomplished by creating either the RTFWordExportFormatOptions or PDFExportFormatOptions objects and calling their setStartPageNumber and setEndPageNumber methods. The resulting object is passed into the setFormatOptions method of the ExportOptions object. The code snippet shown in Listing 28.4 illustrates this.
ExportOptions exportOptions = new ExportOptions(); exportOptions.setExportFormatType(ReportExportFormat.PDF) ; RTFWordExportFormatOptions rtfOptions = new PDFExportFormatOptions(); rtfOptions.setStartPageNumber(1); rtfOptions.setEndPageNumber(3); exportOptions.setFormatOptions(rtfOptions); exporter.setExportOptions(exportOptions);
The other option related to exporting is whether the resulting exported report should be sent back to the browser as an attachment or inline. When sent as an attachment, the browser pops up a dialog asking the user if he would like to save or open the file. This is useful if you think most of your users will want to save the file to their machines. The default behavior is for the report to open inside the browser window in either the Adobe or Microsoft Word embedded viewer. This is controlled via the setExportAsAttachment method of the CrystalReportViewer. This method simply takes a Boolean value, which determines whether the file should be an attachment.
Part I. Crystal Reports Design
Creating and Designing Basic Reports
Selecting and Grouping Data
Filtering, Sorting, and Summarizing Data
Understanding and Implementing Formulas
Implementing Parameters for Dynamic Reporting
Part II. Formatting Crystal Reports
Fundamentals of Report Formatting
Working with Report Sections
Visualizing Your Data with Charts and Maps
Custom Formatting Techniques
Part III. Advanced Crystal Reports Design
Using Cross-Tabs for Summarized Reporting
Using Record Selections and Alerts for Interactive Reporting
Using Subreports and Multi-Pass Reporting
Using Formulas and Custom Functions
Designing Effective Report Templates
Additional Data Sources for Crystal Reports
Multidimensional Reporting Against OLAP Data with Crystal Reports
Part IV. Enterprise Report Design Analytic, Web-based, and Excel Report Design
Introduction to Crystal Repository
Crystal Reports Semantic Layer Business Views
Creating Crystal Analysis Reports
Advanced Crystal Analysis Report Design
Ad-Hoc Application and Excel Plug-in for Ad-Hoc and Analytic Reporting
Part V. Web Report Distribution Using Crystal Enterprise
Introduction to Crystal Enterprise
Using Crystal Enterprise with Web Desktop
Crystal Enterprise Architecture
Planning Considerations When Deploying Crystal Enterprise
Deploying Crystal Enterprise in a Complex Network Environment
Administering and Configuring Crystal Enterprise
Part VI. Customized Report Distribution Using Crystal Reports Components
Java Reporting Components
Crystal Reports .NET Components
COM Reporting Components
Part VII. Customized Report Distribution Using Crystal Enterprise Embedded Edition
Introduction to Crystal Enterprise Embedded Edition
Crystal Enterprise Viewing Reports
Crystal Enterprise Embedded Report Modification and Creation
Part VIII. Customized Report Distribution Using Crystal Enterprise Professional
Introduction to the Crystal Enterprise Professional Object Model
Creating Enterprise Reports Applications with Crystal Enterprise Part I
Creating Enterprise Reporting Applications with Crystal Enterprise Part II
Appendix A. Using Sql Queries In Crystal Reports
Creating Enterprise Reporting Applications with Crystal Enterprise Part II