Exporting Reports to Other File Formats

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.

Exporting via the Toolbar Button

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.

NOTE

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.

Figure 28.3. Export a report through the report viewer.

graphics/28fig03.jpg


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.

Figure 28.4. Here is a report exported to the PDF format.

graphics/28fig04.jpg


Exporting via Code

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.

NOTE

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:

  • ReportExportFormat.PDF (for PDF)
  • ReportExportFormat.RTF (for RTF)

NOTE

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.

Listing 28.3. Exporting via the ReportExportControl

<%@ 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.

Listing 28.4. Specifying Page Numbers When Exporting

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.






Special Edition Using Crystal Reports 10
Special Edition Using Crystal Reports 10
ISBN: 0789731134
EAN: 2147483647
Year: 2003
Pages: 341
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net