Click here to Skip to main content
Click here to Skip to main content

Exporting Crystal Reports in different formats

, 1 Feb 2004
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

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberkalens2-Mar-11 23:22 
GeneralCrystal Report 8 for Export into different format from CRViewer using vb6 code PinmemberManauwar27-Oct-08 1:34 
Generalproblem with method &quot;Export&quot; of CrystalReports PinmemberDevC#3-Aug-05 1:10 
GeneralExport report into text file Pinmembercecosr3-Feb-05 13:14 
QuestionRe: Export report into text file PinmemberBibin Kurian Joy12-Oct-06 20:49 
GeneralExport Options in Crystal Reports from .NET Pinmemberseeyem18-Nov-04 9:57 
GeneralEXPORT TO WORD NOT IS RTF ONLY WHY !!! Pinmemberakorolev1024-Mar-04 20:30 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! PinsussAnonymous24-Mar-04 21:47 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pinmemberseeyem18-Nov-04 9:56 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! PinsussAnonymous8-Nov-04 20:59 
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pinmemberseeyem19-Nov-04 6:12 
AnswerRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! PinmemberBibin Kurian Joy12-Oct-06 20:44 
GeneralNo Source Pinmemberchaldon10-Mar-04 9:54 
GeneralIs it possible to export as CR9 PinsussAnonymous27-Dec-03 19:16 
GeneralRe: Is it possible to export as CR9 PinmemberBibin Kurian Joy22-Dec-06 7:05 
GeneralIt doesn't work to me Pinmemberndiaz25-Nov-03 4:23 
GeneralRe: It doesn't work to me Pinmemberseeyem18-Nov-04 9:52 
QuestionCan not export as PDF or Excel format Pinsusssilverlake28-Oct-03 7:51 
AnswerRe: Can not export as PDF or Excel format PinmemberBibin Kurian Joy22-Dec-06 7:12 
GeneralCool PinsussBlargh27-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 | Mobile
Web01 | 2.8.141022.2 | Last Updated 2 Feb 2004
Article Copyright 2003 by kutubshikora
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid