IntroductionIn 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- SQL Server 2005/2008
- Visual studio 2005/2008/2010
- Crystal report(any versions)
Database DesignLet’s create simple database with one table for this demonstration. The database structure will be like followings, The database name is called HouseRental with HouseOwnerInfo table. There are eight columns inside the HouseOwnerInfo. ImplementationNow 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. 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. 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.
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. Now click next button to see how report design view look, the result as shown below, is 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, Load reports format to DropdownListThe 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 Referenceshttp://www.sap.com/solutions/sapbusinessobjects/sme/reporting/crystalreports/index.epx http://www.codegain.com/articles/crystalreports/ Download Sample ProjectDownload source files -104 kb ConclusionIn 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. |