Click here to Skip to main content
11,642,216 members (61,485 online)
Click here to Skip to main content

Exporting Crystal Reports in different formats

, 1 Feb 2004 118.4K 2.8K 42
Rate this:
Please Sign up or sign in to vote.
An article on exporting Crystal Reports in different formats.

Introduction

This article intends to provide an easy way of incorporating rich reporting functionality into your application using Crystal Reports .NET. Microsoft provides Crystal Report development environment integrated with Visual Studio .NET. It is very easy to develop reports using this environment and also the reports developed in Crystal Reports .Net can be very easily exported in Adobe Acrobat Portable Document Format or Micrsoft word Format by just incorporating few lines of code.

Background

The article addresses readers with basic understanding of crystal reports and who need to understand exporting of crystal report in different formats as selected by the user. The code is a generic code and can be used by changing very few configuration settings as explained below.

Using the code

Before using this code create a report using Crystal Report .Net in Visual Studio by connecting to the database. It is assumed here that the report is developed using pull method i.e. connecting to the database at design time and binding the report fields. The source code provides with the ReportPageBase.cs class, which can be inherited in the pages where the report needs to be displayed. On click of button or on page load call the overloaded function LoadReport, which accepts atleast two arguments report name and report format as ReportExportFormat enumeration. If the report needs filtering or some parameters in the report like dynamic display of title of the report then the same function i.e. LoadReport function with few more arguments has to be used. LoadReport function invokes GenerateReport of the ReportUtility class, which actually generates the report for the supplied arguments.

private void wcbtnGetReport_Click(object sender, System.EventArgs e)
{
    try 
    {
        objCrystalReportViewer.Visible= true;
        LoadReport(wctbReportName.Text, GetReportFormat(
            wclstReportFormat.SelectedItem.Value.ToString()));
    }
    catch(Exception ex)
    {
        Response.Write(ex.Message);
    }
}
        

One of the interesting pieces of code to look at is the database connection code. The code allows the report to connect to database(s) at run time by setting the configuration file. In the configuration file change the values for the items in bold according to the project requirements.

    
<appSettings>
    <add key="ReportServerName" value="10.200.32.216"/>
    <add key="ReportDataBaseName" value="Northwind"/>
    <add key="ReportUserId" value="sa"/>
    <add key="ReportPasswd" value=""/>
    <add key="PhysicalReportPath" value="C:\\inetpub\\wwwroot\\ReportUI\\"/>
    <add key="ReportExportedTempReportPath" value="D:\\Temp\\"/>
</appSettings>
            

So you see, it takes care of the development and deployment environment, if both are different (in most of the cases they are).

LoadReport function invokes GenerateReport of the ReportUtility class, which actually generates the report for the supplied arguments. One of the interesting piece of code to look at is the database connection code. The code allows the report to connect to database(s) at run time. So you see, it takes care of the development and deployment environment, if both are different (in most of the cases they are).

//Added for configuring the DB connection 
foreach(CrystalDecisions.CrystalReports.Engine.Table lrptTable in 
    lrptDocReportDocument.Database.Tables)
{
    lrptTableLogin = lrptTable.LogOnInfo;
    lrptTableLogin.ConnectionInfo.ServerName = 
           ReportConstants.ReportServerName;
    lrptTableLogin.ConnectionInfo.DatabaseName = 
           ReportConstants.ReportDataBaseName;
    lrptTableLogin.ConnectionInfo.UserID = ReportConstants.ReportUserId;
    lrptTableLogin.ConnectionInfo.Password = ReportConstants.ReportPasswd;
    lrptTableLogin.TableName = lrptTable.Name;
    lrptTable.ApplyLogOnInfo(lrptTableLogin);
    lrptTable.Location = lrptTable.Name;
}
            

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

kutubshikora
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
kalens2-Mar-11 23:22
memberkalens2-Mar-11 23:22 
GeneralCrystal Report 8 for Export into different format from CRViewer using vb6 code Pin
Manauwar27-Oct-08 1:34
memberManauwar27-Oct-08 1:34 
Generalproblem with method &quot;Export&quot; of CrystalReports Pin
DevC#3-Aug-05 1:10
memberDevC#3-Aug-05 1:10 
GeneralExport report into text file Pin
cecosr3-Feb-05 13:14
membercecosr3-Feb-05 13:14 
QuestionRe: Export report into text file Pin
Bibin Kurian Joy12-Oct-06 20:49
memberBibin Kurian Joy12-Oct-06 20:49 
GeneralExport Options in Crystal Reports from .NET Pin
seeyem18-Nov-04 9:57
memberseeyem18-Nov-04 9:57 
GeneralEXPORT TO WORD NOT IS RTF ONLY WHY !!! Pin
akorolev1024-Mar-04 20:30
memberakorolev1024-Mar-04 20:30 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pin
Anonymous24-Mar-04 21:47
sussAnonymous24-Mar-04 21:47 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pin
seeyem18-Nov-04 9:56
memberseeyem18-Nov-04 9:56 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pin
Anonymous8-Nov-04 20:59
sussAnonymous8-Nov-04 20:59 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pin
seeyem19-Nov-04 6:12
memberseeyem19-Nov-04 6:12 
AnswerRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pin
Bibin Kurian Joy12-Oct-06 20:44
memberBibin Kurian Joy12-Oct-06 20:44 
GeneralNo Source Pin
chaldon10-Mar-04 9:54
memberchaldon10-Mar-04 9:54 
GeneralIs it possible to export as CR9 Pin
Anonymous27-Dec-03 19:16
sussAnonymous27-Dec-03 19:16 
GeneralRe: Is it possible to export as CR9 Pin
Bibin Kurian Joy22-Dec-06 7:05
memberBibin Kurian Joy22-Dec-06 7:05 
GeneralIt doesn't work to me Pin
ndiaz25-Nov-03 4:23
memberndiaz25-Nov-03 4:23 
GeneralRe: It doesn't work to me Pin
seeyem18-Nov-04 9:52
memberseeyem18-Nov-04 9:52 
QuestionCan not export as PDF or Excel format Pin
silverlake28-Oct-03 7:51
susssilverlake28-Oct-03 7:51 
AnswerRe: Can not export as PDF or Excel format Pin
Bibin Kurian Joy22-Dec-06 7:12
memberBibin Kurian Joy22-Dec-06 7:12 
GeneralCool Pin
Blargh27-Oct-03 8:27
sussBlargh27-Oct-03 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 2 Feb 2004
Article Copyright 2003 by kutubshikora
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid