How to Export Crystal Report to Different Formats (PDF,Excel,Word) using C#

No.of Views22911
Bookmarked1 times
Downloads 
Votes0
By  youngmurukan   On  15 Feb 2010 21:02:15
Tag : Crystal Reports , File Formats
In this article, i will implement how to export Crystal report to different files format using C#.The Crystal report is powerful tool for create report and deploy with applications.but sometimes we may need to export it to PDF,Excel,Word and more.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, i will implement how to export Crystal report to different files format using C#.The Crystal report is powerful tool for create report and deploy with applications.but sometimes we may need to export it to PDF,Excel,Word and more.

The Crystal report has a feature to export data to different file formats without viewing report on CrystalReportViewer. In this article I will demonstrate how we can export report to different file formats by programmatically. Hence we will not show the report on the screen but at the same time save in the file system with different file exported formats.

Prerequisites

  1. SQL Server 2005/2008
  2. Visual studio 2005/2008/2010
  3. Crystal report(any versions)

 

Database Design

Let’s create simple database with one table for this demonstration. The database structure will be like followings, 

Image Loading

The database name is called HouseRental with HouseOwnerInfo table. There are eight columns inside the HouseOwnerInfo.

Implementation

Now I’m going to use this table and design report for present summary for house owner details.Just for your information, there are two ways to design report,

1. Using Crystal report Editor
2. Using Visual studio Editor

But in this demonstration I using Visual studio editor to create and design report.

Let’s open VS and create new windows project called as ExportReportToDiffFileFormat. Then right click the project and select Add New Item from the bar. Select Crystal Report from the templates list window. When you select crystal report, give a meaningful name to the report. Here I have given DiffFileFormatRpt.rpt. Then click OK button.desgin wizard is ready. 

Image Loading

In this window, you have to select which report template format suitable for our report. In this demonstration standard report is 100% suitable. Select and click OK button.

Then you need to create the database connection to access tables within the database. Once you click OK button, system gives a window to select the connection type as shown below.

Image Loading

This demonstration report is to work with SQL Server 2005 database. So you need to select SQL native client as provider (As above figure shows).


Then click Next button, to continue report creation process. Then you need to assign details of SQL Server and user credential to access the database from the report. Then you need to expand current connection tag, select your database connection then select the relevant table as shown by the following figure. 

Image Loading

Then click Next button to continue. As shown in below figure you have to select the columns name to design report from you have selected tables in previous step.

Image Loading

Now click next button to see how report design view look, the result as shown below, is 

Image Loading

The report design is finished, now navigate to form1 in the windows project and design a simple GUI to select export formats list using dropdownlist  and add a button to export it when user clicks. Windows form as like below,

Image Loading

Load reports format to DropdownList

The crystal report has enumeration list for support report formats. So I’m going to that to load formats to dropdownlist using few lines for code within the form load event.

C# Code

private void frmMain_Load(object sender, EventArgs e)
{

string[] collectionFormat = Enum.GetNames(typeof(CrystalDecisions.Shared.ExportFormatType));
this.cmbFileFomats.Items.AddRange(collectionFormat);

}

In above code, i just get the supported formats names list from the CrystalDecisions.Shared.ExporeFormatType enum and assign into to an array then add all items within the array into the dropdownlist.

Next compose code for button click event,

C# Code

private void button1_Click(object sender, EventArgs e)
        {
            ReportDocument report = new ReportDocument();
            report.Load("DiffFileFormatRpt.rpt");
            report.SetDatabaseLogon("username", "password", @"server", "database");
            string filetype = cmbFileFomats.Text;
            CrystalDecisions.Shared.ExportFormatType efileType = (CrystalDecisions.Shared.ExportFormatType)Enum.Parse(typeof(CrystalDecisions.Shared.ExportFormatType), filetype);
            switch (efileType)
            {
                case CrystalDecisions.Shared.ExportFormatType.Excel:
                    report.ExportToDisk(efileType, "reportExcel.xls");
                    break;
                case CrystalDecisions.Shared.ExportFormatType.ExcelRecord:
                    report.ExportToDisk(efileType, "reportExcel.xls");
                    break;
                case CrystalDecisions.Shared.ExportFormatType.HTML32:
                    report.ExportToDisk(efileType, "reporthtml.html");
                    break;
                case CrystalDecisions.Shared.ExportFormatType.HTML40:
                    report.ExportToDisk(efileType, "reporthtml.html");
                    break;
                case CrystalDecisions.Shared.ExportFormatType.NoFormat:
                    report.ExportToDisk(efileType, "reportExcel.xls");
                    break;
                case CrystalDecisions.Shared.ExportFormatType.PortableDocFormat:
                    report.ExportToDisk(efileType, "reportpdf.pdf");
                    break;
                case CrystalDecisions.Shared.ExportFormatType.RichText:
                    report.ExportToDisk(efileType, "reportrtf.rtf");
                    break;
                case CrystalDecisions.Shared.ExportFormatType.WordForWindows:
                    report.ExportToDisk(efileType, "reportdoc.doc");
                    break;
            }
        }

In above code line are straight forward and simple. I have create report document object and then set designed report file to report object

ReportDocument report = new ReportDocument();
report.Load("DiffFileFormatRpt.rpt");

Note:The report file path you have to give correctlly, otherwise you will get error "The report file not found".

And then set database connection to report object. This is not best practices, but for this demonstration purpose I have done like that. The best practices create dataset or object with records from the database and set that as DataSource to report. Then you don’t need set directly database connection credential details.

report.SetDatabaseLogon("username", "password", @"server", "database");

 Then just use the case statement to export report as user selected from the dropdownlist.

CrystalDecisions.Shared.ExportFormatType efileType = (CrystalDecisions.Shared.ExportFormatType)Enum.Parse(typeof(CrystalDecisions.Shared.ExportFormatType), filetype);
            switch (efileType)
            {
                case CrystalDecisions.Shared.ExportFormatType.Excel:
                    report.ExportToDisk(efileType, "reportExcel.xls");
                    break;
.........
}

Finally to save exported format into disk, you have to call ExportToDisk().

report.ExportToDisk(efileType, "reportExcel.xls");

Outputs

PDF and Word Formats

 

Image Loading

References

http://www.sap.com/solutions/sapbusinessobjects/sme/reporting/crystalreports/index.epx

http://www.codegain.com/articles/crystalreports/

Download Sample Project

Download source files -104 kb

Conclusion

In this article, you have learned how to export Crystal report to different files format using C#.The Crystal report is powerful tool for create report and deploy with applications.but sometimes we may need to export it to PDF,Excel,Word and more.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
youngmurukan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 May 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
By:NaveenDate Of Posted:12/16/2012 8:48:39 PM
It is nice article
Hi, It is wonder full article that save my time a lot.
By:leeDate Of Posted:2/3/2012 12:55:29 PM
Export
Export to PDF http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-export-pdf.htm
By:anilDate Of Posted:4/22/2010 7:24:07 AM
changes
Change paramDTDefinition.ApplyCurrentValues(currentcValues); to paramDOPDefinition.ApplyCurrentValues(currentcValues);
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top