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.


Comments

Author: Muzaffar24 Jan 2012 Member Level: Bronze   Points : 0

How to export that .pdf file to a user defined folder location with user defined name

Author: Himanshu Patel24 Jan 2012 Member Level: Gold   Points : 2

Take one SaveFile Dialog control...
Use below code to fulfill your requirement:


saveFileDialog1.Filter = "*.pdf|(Pdf File)";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{

crystalReportDoc.ExportToDisk(ExportFormatType.PortableDocFormat, saveFileDialog1.FileName + ".pdf");

MessageBox.Show("Report Exported Succesful..");

}

Author: Vivek Srivathsan05 Mar 2012 Member Level: Bronze   Points : 0

hi there ,
am getting error in the header file itself

Cheers

Author: Himanshu Patel07 Mar 2012 Member Level: Gold   Points : 0

Hi If you are using Visual Studio 2010 then you must have to install Crystal Reports Separately.
Once installing you can add reference to your Project.

Guest Author: balamurugan10 Dec 2012

error : Load report failed.

sda = new SqlDataAdapter("SELECT details.Memberno, details.Firstname, details.Socendname, details.DOB, samp.sno, samp.CName FROM details INNER JOIN samp ON details.icno = samp.icno where details.Memberno='" + txtGetValue.Text + "'", con);
sda.Fill(ds);

ReportDocument rpdoc = new ReportDocument();
rpdoc.Load("cptMemberDetails.rpt");
rpdoc.SetDataSource(ds);

rpdoc.ExportToDisk(ExportFormatType.PortableDocFormat, "Reports.pdf");



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: