Exporting Reports


Exporting Reports

Although a lot of time and effort has gone into developing a Crystal report, users of your JSP application will want to have the report in a format that enables them to manipulate the report using various office software products. You can provide a Crystal report in Rich Text Format or Acrobat PDF format (these are the only formats supported by the initial release of the Java Reporting Component) by writing a JSP application that exports a Crystal report into one of those formats. First you need to create a report source as described in the preceding section of this chapter, and then you ll need to create and initialize an export control. The export control takes away the headaches of writing code that exports a Crystal report into another format.

Methods needed to export a Crystal report are defined in the ReportExportControl class. You create an instance of this class by using the following statement in your JSP application:

 ReportExportControl exportControl = new ReportExportControl(); 

Once you ve created an instance of the ReportExportControl, you ll need to create an instance of the ExportOptions class. The ExportOptions class defines methods that are used to set export options. One of those options is to designate an export format for the Crystal report. Here s the statement that is required to declare an instance of the ExportOptions class:

 ExportOptions exportOptions = new ExportOptions(); 

Next, specify the format used to export the Crystal report. This is done by calling the setExportFormatType() method. The setExportFormatType() method requires one argument, which is the ReportExportFormat constant for the export format.

There are two export formats. These are Rich Text Format (RTF) and Acrobat Reader format (PDF). You need to use the ReportExportFormat.RTF constant to specify that you want to export the Crystal report to the Rich Text Format. The ReportExportFormat.PDF is used to export the Crystal report to the Acrobat Reader Format. Here s how to specify the export format. This example exports the Crystal report to the Acrobat Reader Format:

 exportOptions.setExportFormatType(ReportExportFormat.PDF); 

Each export format has its own set of options that you can set. One of the most popular options is to specify the range of pages of the Crystal report to export into the specified format. In order to set the export format options, you need to create an instance of either the RTFWordExportFormatOptions class or the PDFWordExportFormatOptions class, as shown here:

 PDFWordExportFormatOptions PDFOpts = new PDFWordExportFormatOptions(); 
RTFWordExportFormatOptions RTFOpts = new RTFWordExportFormatOptions();

Once the instance is declared, you can call the appropriate method to set the specific option that you want to set. For example, here s how you set the range of pages of the Crystal report to export. These methods cause pages 5 through 10 to be exported in the PDF format. These same methods are available for RTF format:

 PDFOpts.setStartPageNumber(5); 
PDFOpts.setEndPageNumber(10);

The format options are set separate from the export options. Therefore, you need to associate the format options with the export options. You do this by calling the setFormatOptions() method and passing it a reference to the format options instance, as shown here:

 exportOptions.setFormatOptions(PDFExpOpts); 

Export options must be associated with an instance of the ReportExportControl class, which you already declared previously in this section. You draw this association by calling the setExportOptions() method and passing it a reference to the instance of the ReportExportControl class. This is shown here:

 exportControl.setExportOptions(exportOptions); 

Next, you need to associate a report source with the instance of the ReportExportControl class. This is done by calling the setReportSource() method and passing it a reference to the report source, which is shown in the following statement:

 exportControl.setReportSource(rptSource); 

The final step is to actually export the Crystal report into the specified format. All you need to do is call the processHttpRequest() method, as shown here.

 exportControl.processHttpRequest(request, response, getServletContext(), null); 

The processHttpRequest() method causes the Crystal report to be displayed in the export format within the browser window. However, users of your JSP application will often prefer having the option to view the report on the screen or save the report file to their hard drive so that they can review the report at a later date without having to use your JSP application again.

You can give your users this option by calling the setExportAsAttachment() method before you call the processHttpRequest() method. The setExportAsAttachment() method is used to specify whether or not the user has the option to save the exported report to a hard disk. This is referred to as exporting the file as an attachment, which is similar to an e-mail attachment.

The setExportAsAttachment() method requires one Boolean argument. A Boolean value of true means that the exported file is treated as an attachment. When you call the processHttpRequest() method, a dialog box is displayed on the screen prompting the user to decide whether to review the report within the browser window or to save the report to a hard disk. This is the same dialog box that you see whenever you select an attachment to an e-mail. Passing a false argument causes the report to be displayed on the screen only.

The following example reiterates concepts covered in this section into a JSP application that exports a Crystal report:

 <%@ page import="com.crystaldecisions.report.web.viewer.ReportExportControl" %> 
<%@ page import=
"com.crystaldecisions.reports.reportengineinterface.JPEReportSourceFactory,
com.crystaldecisions.sdk.occa.report.reportsource.IReportSourceFactory2,
com.crystaldecisions.reports.reportengineinterface.IReportSource" %>
<%@ page import=
"com.crystaldecisions.sdk.occa.report.exportoptions.ExportOptions" %>
<%@ page import=
"com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat" %>
<%@ page import=
"com.crystaldecisions.sdk.occa.report.exportoptions.
IRTFWordExportFormatOptions" %>
<%
IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory();
IReportSource rptSource = (IReportSource) rptSrcFactory.createReportSource
("/reports/sample.rpt", request.getLocale());
ReportExportControl exportControl = new ReportExportControl();
ExportOptions exportOptions = new ExportOptions();
exportOptions.setExportFormatType(ReportExportFormat.PDF);
PDFWordExportFormatOptions PDFOpts = new PDFWordExportFormatOptions();
PDFOpts.setStartPageNumber(5);
PDFOpts.setEndPageNumber(10);
exportOptions.setFormatOptions(PDFOpts);
exportControl.setReportSource(rptSource);
exportControl.setExportOptions(exportOptions);
exportControl.setExportAsAttachment(true);
exportControl.processHttpRequest(request, response, getServletConfig().
getServletContext(), null);
exportControl.dispose();
%>



Crystal Reports 10
Crystal Reports 10: The Complete Reference
ISBN: B005DI80VA
EAN: N/A
Year: 2004
Pages: 223
Authors: George Peck

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