Sams Teach Yourself Visual Studio .NET 2003 in 21 Days

Sams Teach Yourself .Net in 21 Days

By Jason Beres

Exporting a Report Programmatically

Another useful feature is the ability to export a report document without the end user viewing it first and then clicking the Export button on the toolbar. To do this, you use the ExportOptions collection of the ReportDocument class to set properties on what you want to export and how it should be exported. The code in Listing 18.3 exports a report file to a PDF file and saves it to disk.

Example 18.3. Exporting a Report with Code Using the Export Method

vbnet_icon.gif
Private Sub button1_Click(sender As Object, e As System.EventArgs)

   ' The path/location where the exported file will be saved
   Dim exportFilePath As String = "c:\Report1.pdf"

   ' Create an instance of the untyped report object
   oRpt = New ReportDocument()

   ' Load the report from disk
   oRpt.Load(reportFile)

   ' Set the options for saving the exported file to disk
   oDest = New DiskFileDestinationOptions()
   oDest.DiskFileName = exportFilePath

   ' Set the exporting information
   oExport = oRpt.ExportOptions

   ' Set the destination options
   oExport.DestinationOptions = oDest

   ' Set the location, this can be:
   ' DiskFile, ExchangeFolder, MicrosoftMail or NoDestination
   oExport.ExportDestinationType = ExportDestinationType.DiskFile

   ' Set the Export type, this can be:
   ' PDF, Excel, Word Doc, RTF Doc,
   ' HTML 3.2, HTML 4.0 or CrystalReport
   oExport.ExportFormatType = ExportFormatType.PortableDocFormat

   ' Call the Export method to export the report
   oRpt.Export()
   MessageBox.Show("Report Exported!")
End Sub
c_icon.gif
   private void button1_Click(object sender, System.EventArgs e)
   {

      // The path/location where the exported file will be saved
      string exportFilePath = "c:\\Report1.pdf";

      // Create an instance of the untyped report object
      oRpt = new ReportDocument();

      // Load the report from disk
      oRpt.Load(reportFile);

      // Set the options for saving the exported file to disk
      oDest = new DiskFileDestinationOptions();
      oDest.DiskFileName = exportFilePath;

      // Set the exporting information
      oExport = oRpt.ExportOptions;

      // Set the destination options
      oExport.DestinationOptions = oDest;

      // Set the location, this can be:
      // DiskFile, ExchangeFolder, MicrosoftMail or NoDestination
      oExport.ExportDestinationType = ExportDestinationType.DiskFile;

      // Set the Export type, this can be:
      // PDF, Excel, Word Doc, RTF Doc,
      // HTML 3.2, HTML 4.0 or CrystalReport
      oExport.ExportFormatType = ExportFormatType.PortableDocFormat;

      // Call the Export method to export the report
      oRpt.Export();
      MessageBox.Show("Report Exported!");

   }
}

In the commented code in Listing 18.3, you can see the various options for the ExportFormatType and the ExportDestinationType for the report. These are the same properties that you can set in the viewer control.

Share ThisShare This

Informit Network