Crystal Reports - Exporting to Various Formats
Exporting Crystal Reports to PDF, Word, Excel
This article will demonstrate how to automate the export of a Crystal Report from an ASP .Net page into a formatted type such as PDF, Word or Excel. I have also provided sample code to Export a Report to the hard drive as a PDF file stored on the disk in a folder.
Crystal Reports Standard Export Techinique
Exporting a Crystal Report from the Browser is not a difficult task for those of us who know our way around Business Objects products. Simply click on the Export button.
Select the format that you need the Exported Crystal Report.
And it pops up in its own Browser window ready for you to save.
However, I have found that it is sometimes easier to provide the report in a format that removes these extra steps for the user so they can get their report without any problems.
For this article demonstration, a quick summary\chart report was created from the AdventureWorks Database using the view Sales.vSalesPersonSalesByFiscalYears.
Crystal Reports Export to PDF, Word, or Excel
In the Default.aspx.cs file, the following code is added to force the report into a PDF Format on Page_Load:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
ReportDocument crRpt = new ReportDocument();
crRpt.Load(Server.MapPath("CrystalReport2.rpt"));
crRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, "CrystalReport2");
crRpt.Dispose();
Running the web page will produce the report directly into a PDF Format.
Replacing the code "ExportFormatType.Excel" opens an Excel version of the report.
Replacing the "ExportFormatType.WordForWindows" produces the Wordversion of Report.
Exporting Crystal Reports to the Hard Disk Drive
You could also Export the Crystal report in any of the above formats to the hard drive as a report that could be sent or shared. The following code shows how to create the Crystal Report, Load it, export it to a file named myfileName.pdf, assign the DiskFileDestinationOptions properties, and finally export the report.
ReportDocument crRpt = new ReportDocument();
crRpt.Load(Server.MapPath("CrystalReport2.rpt"));
CrystalDecisions.Shared.ExportOptions myExportOptions;
CrystalDecisions.Shared.DiskFileDestinationOptions myDiskFileDestinationOptions;
string myExportFile;
myExportFile = @"C:\test\myfileName.pdf";
myDiskFileDestinationOptions = new CrystalDecisions.Shared.DiskFileDestinationOptions();
myDiskFileDestinationOptions.DiskFileName = myExportFile;
myExportOptions = crRpt.ExportOptions;
myExportOptions.DestinationOptions = myDiskFileDestinationOptions;
myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
myExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
//create Crystal Report locally
crRpt.Export();
A PDF version of the report is created in the C:\test folder following program execution.
Crystal Reports Fails On Load Event
On your local system, you may find that you encounter an error on the Load event for the report. The temporary report is created on C:\Windows\temp\. The Network Service Account must be added and given Modify and Write permissions to the C:\Windows\Temp\ folder to "fix" this problem.