Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET
Article

Exporting Crystal Reports in different formats

Rate me:
Please Sign up or sign in to vote.
2.40/5 (15 votes)
1 Feb 2004Ms-RL2 min read 139.2K   3K   42   20
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.

C#
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.

XML
<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).

C#
//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, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

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

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