Home

C#

Vb.Net

Java

Sharepoint

Php

Action Script






Active Directory Utility Class




Downloads
Technology News
Templates
Web Hosting
Articles
Games
Blogger
Google





Wednesday, September 24, 2008
How to export to Crystal Reports format from a VS .NET application
How to export to Crystal Reports format from a VS .NET application

How do you export a report to Crystal Reports format from a .NET application (Windows or web) using VB .NET or C# code?
Resolution

To export a report to Crystal Reports format from a .NET application perform the following steps: Read more...

====================

NOTE:

The following steps will show both VB .NET and C# code samples.

====================

1. Add the assemblies.

Three assemblies must be added; CrystalDecisions.CrystalReports.Engine.dll, CrystalDecisions.ReportSource.dll and CrystalDecisions.Shared.dll. To add them, place the following code at the top of the code page:

VB. NET:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

C#:

Using CrystalDecisions.CrystalReports.Engine;
Using CrystalDecisions.Shared;

2. Define a location to save the file to.

Place the following code in the routine that will process the report (For example the Page_Load or Button_Click event):

VB. NET:

Dim exportFileName As String = "exportedReport.rpt"
Dim exportPath As String = Application.StartupPath & "\" & exportFileName

C#:

string exportFileName = "exportedReport.rpt";
string exportPath = Application.StartupPath + "\\" + exportFileName;

3. Load the report.

The following code instantiates a ReportDocument object and loads a report from disk.

VB. NET:

Dim crReportDocument As New ReportDocument
crReportDocument.Load("path_to_your_report_file.rpt")

C#:

ReportDocument crReportDocument = new ReportDocument();
crReportDocument.Load("path_to_your_report_file.rpt");

4. Export the report to disk.

There are two methods to export a report to disk. The first is to use the 'ExportOptions' class to define the exporting options while the second is to use the 'ReportDocument.ExportToDisk' shortcut method. Both are described below.

Method 1 - Using the 'ExportOptions' Class

---------------------------------------------------

VB. NET:

Dim crExportOptions As ExportOptions
Dim crDestOptions As New DiskFileDestinationOptions
crDestOptions.DiskFileName = exportPath
crExportOptions = crReportDocument.ExportOptions
crExportOptions.DestinationOptions = crDestOptions
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
crExportOptions.ExportFormatType = ExportFormatType.CrystalReport
crReportDocument.Export()

C#:

ExportOptions crExportOptions;
DiskFileDestinationOptions crDestOptions = new DiskFileDestinationOptions();
crDestOptions.DiskFileName = exportPath;
crExportOptions = crReportDocument.ExportOptions;
crExportOptions.DestinationOptions = crDestOptions;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.CrystalReport;
crReportDocument.Export();

Method 2 - Using the 'ExportToDisk' Method

----------------------------------------------------

VB.NET:

crReportDocument.ExportToDisk(ExportFormatType.CrystalReport, exportPath)

C#:

crReportDocument.ExportToDisk(ExportFormatType.CrystalReport, exportPath);

Once the code is executed successfully, you will find the exported report in Crystal Report (.RPT) format in the folder as specified by the 'exportPath' variable, which in this case is the folder with the running executable (.exe).

Labels:

0 Comments:

Post a Comment

<< Home