Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Exporting Crystal Reports in different formats

0.00/5 (No votes)
1 Feb 2004 2  
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