Click here to Skip to main content
Licence 
First Posted 26 Oct 2003
Views 109,345
Bookmarked 40 times

Exporting Crystal Reports in different formats

By | 1 Feb 2004 | Article
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

About the Author

kutubshikora

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberkalens23:22 2 Mar '11  
GeneralCrystal Report 8 for Export into different format from CRViewer using vb6 code PinmemberManauwar1:34 27 Oct '08  
Generalproblem with method "Export" of CrystalReports PinmemberDevC#1:10 3 Aug '05  
GeneralExport report into text file Pinmembercecosr13:14 3 Feb '05  
QuestionRe: Export report into text file PinmemberBibin Kurian Joy20:49 12 Oct '06  
GeneralExport Options in Crystal Reports from .NET Pinmemberseeyem19:57 8 Nov '04  
GeneralEXPORT TO WORD NOT IS RTF ONLY WHY !!! Pinmemberakorolev1020:30 24 Mar '04  
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! PinsussAnonymous21:47 24 Mar '04  
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pinmemberseeyem19:56 8 Nov '04  
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! PinsussAnonymous20:59 8 Nov '04  
GeneralRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! Pinmemberseeyem16:12 9 Nov '04  
AnswerRe: EXPORT TO WORD NOT IS RTF ONLY WHY !!! PinmemberBibin Kurian Joy20:44 12 Oct '06  
GeneralNo Source Pinmemberchaldon9:54 10 Mar '04  
GeneralIs it possible to export as CR9 PinsussAnonymous19:16 27 Dec '03  
GeneralRe: Is it possible to export as CR9 PinmemberBibin Kurian Joy7:05 22 Dec '06  
GeneralIt doesn't work to me Pinmemberndiaz4:23 25 Nov '03  
GeneralRe: It doesn't work to me Pinmemberseeyem19:52 8 Nov '04  
QuestionCan not export as PDF or Excel format Pinsusssilverlake7:51 28 Oct '03  
AnswerRe: Can not export as PDF or Excel format PinmemberBibin Kurian Joy7:12 22 Dec '06  
GeneralCool PinsussBlargh8:27 27 Oct '03  

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.

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