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.
Listing 18.3 Exporting a Report with Code Using the Export Method
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
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.
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.