Export Crystal Reports Dynamically in C#.NET Windows Application
This resource is all about exporting Crystal Report in different formats like pdf, doc, excel etc dynamically in C# Windows Application.
Here I am showing you two different ways to export Crystal Report Dynamically to Hard Disk of the System.
You can export Crystal Report without using Crystal Report Viewer Control.
Here I am going to see you that how to export Crystal Report dynamically in different file Formats with two different methods.
First with CrystalReport1.rpt's Export() and second with ExportToDisk() method.Namespaces required:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;Export Crystal Report To Excel (*.xls ) File Format:
Create the ExportOptions class object and DiskFileDestinationOptions class object.
Set Disk File Name to which excel file is going to be exported. Don't forget to add .xls file extension.
In ExportOptions set ExportDestinationType to DiskFile.
Set ExportFormatType to Excel type.
CrystalReport1 report=new CrystalReport1();
//Bind Data to report…
report.SetDataSource(data);// data is Dataset or DataTable.
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
CrDiskFileDestinationOptions.DiskFileName = "ReportName.xls";
CrExportOptions = report.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.Excel;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = ExportOptions.CreateExcelFormatOptions();
}
report.Export();Export Crystal Report To Portable Doc Format (*.pdf ) File Format:
Create the ExportOptions class object, PdfRtfWordFormatOptions and DiskFileDestinationOptions class object.
Set Disk File Name to which excel file is going to be exported. Don't forget to add .pdf/.doc/.rtf file extension.
Using PdfRtfWordFormatOptions class We can export Crystal Report to three different file formats pdf doc and rtf.
In ExportOptions set ExportDestinationType to DiskFile.
Set ExportFormatType to PortableDocFormat/WordForWindows/RichText type.
Here is an example of PortableDocFormat.
CrystalReport1 report=new CrystalReport1();
//Bind Data to report…
report.SetDataSource(data);// data is Dataset or DataTable.
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = "ReportName.pdf";
CrExportOptions = report.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
report.Export();Simplest Way to Export Crystal Report To Excel (*.xls ) File Format to Disk:
This is the simplest way to export Crystal Report to Disk using ReportDocument class.
First Create the object of ReportDocument class. Then Load Report into this.
Bind data to ReportDocument object.
Then Use ExportToDick method to Export.
Set first argument as Export Type to Excel and Second argument to destination file.
ReportDocument doc = new ReportDocument();
doc.Load("CrystalReport1.rpt");
doc.SetDataSource(data); // data is Dataset or DataTable.
doc.ExportToDisk(ExportFormatType.Excel, "ReportName.xls");Simplest Way to Export Crystal Report To Portable Doc Format (*.pdf ) File Format to Disk:
ReportDocument doc = new ReportDocument();
doc.Load("CrystalReport1.rpt");
doc.SetDataSource(data); // data is Dataset or DataTable.
doc.ExportToDisk(ExportFormatType.PortableDocFormat, "ReportName.pdf");
Same way we can export Crystal Report to Rich Text(rtf), Word File(doc), Crystal Report(rpt) and Html format.
How to export that .pdf file to a user defined folder location with user defined name