Export report different file formats

No.of Views1598
Bookmarked0 times
Downloads 
Votes0
By  youngmurukan   On  15 Feb 2010 21:02:15
Tag : Crystal Reports , How to
Export report different file formats
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

 

Abstract:

The Crystal report has a feature to export data to different file formats without viewing report on CrystalReportViewer. Here I like to 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 we can save the report file format.

Introduction

The purpose of this article is to illustrate how to export reports different files types. As I mentioned my earlier articles reporting is most important, may be users need reports in different file types. Crystal report also has feature to export report to various file formats.

Prerequisites:

1. SQL Server 2005

2. Visual studio 2005/2008

3. Crystal report

Before start to design a report, you need a database which is going to use by report. Here I have designed a small database for this demonstration.

The created database name is "HouseRental" with "HouseOwnerInfo" table. There are eight columns in the "HouseOwnerInfo.

Let us design a report to this table, get 'House owners' summary report' which is using date parameter to process the report

Note: The database design is only for demonstration purpose.

Let us start designing the report. For that, there are two standard ways. The first way is we can use the crystal report and second one is we can use the Visual studio. Here I have used Visual Studio since I prefer that.

First open Visual studio 2008 or 2005 and create new web project called "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.

Once you click the OK button, you will get a welcome window as shown by the following figure.

Keep the Standard report format 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. There you need to select fields from selected tables to design report as shown by the following figure.

Then click Next button. Now you have done necessary effort and finished the final step. As a result, you will get report like this.

Let us work with some coding to archive our goal.

We have created a report in the earlier steps, now we need to write code for export report to different files type according to file types of user's selection.

Now design a straightforward user interface to select export file format.

Let us write code for Button click and form load event.

First we need to load all file types to drop-down list on the form load.

{codecitation class="brush: c#; gutter: true;" width="700px"}

private void frmMain_Load(object sender, EventArgs e)

{

string[] collectionFormat = Enum.GetNames(typeof(CrystalDecisions.Shared.ExportFormatType));

this.cmbFileFomats.Items.AddRange(collectionFormat);

}

{/codecitation}

Here we have just loaded get enumeration for all supportable formats by the crystal report and make it to a string collection and then bind to the drop down list.

Then write code for button click event, to export report to selected file format. (from the drop down list)

{codecitation class="brush: c#; gutter: true;" width="700px"}

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;

}

}

{/codecitation}

Here we have loaded report from the source file and then we need to set database logon to system. After that you need to call one of the methods in the report object, to export report.

{codecitation class="brush: c#; gutter: true;" width="700px"}

case CrystalDecisions.Shared.ExportFormatType.Excel:

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

{/codecitation}

Then write switch case statement to switch path according to the file type selection.

Now build the application and run. Then select a report format and click 'Make it' button to process.

Finally here are few sample outputs.

Pd

Word format output

Conclusion:

In this article we have learned to convert reports to different formats with CrystalReportViewer. Now you know the conversion is easy with crystal report and .net

References:

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

2.http://www.c-sharpcorner.com/Articles/ArticleListing.aspx?SectionID=1&SubSectionID=61

 
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
There is no comments for this articles.
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