Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

19.05 Advanced Exporting

Advanced Exporting

When you want to do more than a simple export to disk, use the Export() method. It has many classes and properties for customizing the export process. Consequently, having additional classes and properties makes this method more complicated to learn and use. This chapter shows plenty of examples so that it is easy to copy and paste the code into your application needs.

Even though the Export() method gives you the most flexibility for exporting reports, it doesn’t let you export to a stream object. Only the ExportToStream() method lets you do that. The ironic part is that the ExportToStream() method is simplistic like the ExportToDisk() method and it doesn’t give you any the formatting options.

Prior to calling the Export() method, you have to create the proper objects that specify what the destination and format are. Assign these objects to properties of the report object. The two properties you have to set are called ExportDestinationType and ExportFormatType. Table 19-3 shows the enumerations for the ExportDestinationType property. Each is in the namespace CrystalDecisions.Shared.ExportDestinationType. Using these properties in your code is illustrated in the code samples that follow.

Table 19-3. Enumeration constants for ExportDestinationType.

Destination Enumeration Constant
Disk File DiskFile
Exchange Folder ExchangeFolder
Email (MAPI) MicrosoftMail

Table 19-4. Enumeration constants for the ExportFormatType property.

Format Enumeration Constant
Disk File DiskFile
Exchange Folder ExchangeFolder
Email (MAPI) MicrosoftMail

Exporting a report consists of five steps. First create the export objects and set their properties. Lastly, call the Export() method.

1. Set the ExportDestinationType property.
2. Create a destination options object and assign it to the Export-DestinationOptions property.
3. Set the ExportFormatType property to the format.
4. Create a format options object and assign it to the ExportFormatOptions property.
5. Call the Export() method of the report object.

To make these steps easy to learn, each one is described for every possible option and the complete code is shown. Listing 19-1 shows sample code of how you can use set these properties in your application. This code serves as the foundation for your own application. You have to modify this code to call the procedures you need. For example, rather than calling the method to export to PDF, you might want to call the method that exports to HTML. Each of the different export methods is discussed in the following sections.

Listing 19-1. Export to a PDF disk file
Dim myReport As New CrystalReport1
'Export the report as destination type disk
SetDiskFileDestination(myReport, "C:\ReportExport.PDF")
'Set the format to be PDF and export all pages
SetFormatPdfRtfWord(myReport, False, 1, 1)
'Perform the export
myReport.Export()

In this listing, a report object is instantiated by creating a new instance of the report you are working with. In your application, you don’t have to load the report into memory this way. This is strictly for showing a simple example. You have the option to load a report using whatever way is most appropriate for your project (e.g. loading an external report file).

After the report is loaded in memory, call the SetDestinationxxx() method that you need. This example uses the SetDestinationDisk() method to export the report to a disk file. You can replace this with the method call you need.

Call the SetFormatxxx() method to set the properties for how the report should be formatted. Again, you can use any formatting method that you need. They are shown in later section.

The last step calls the Export() method of the report object. This exports the report and the user can go back to previewing the report or you can dispose of the report object if you no longer need it.