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

How to export a Crystal Report using C#

Rate me:
Please Sign up or sign in to vote.
2.38/5 (16 votes)
11 May 20073 min read 130.1K   26   11
This article describes how to use C# to customize exporting a Crystal Report

Introduction

I had been searching for some good code that allows a programmer to export a Crystal Report programmatically, without using the (not always) functional buttons that come with Crystal Reports. This is a short, straight forward article on how to export a Crystal Report in the most common formats. The code is C# in an ASP.NET codebehind file.

I am using the export through a Drop Down List, which I have pre-populated with 4 values:

1 - Rich text format

2 - PDF

3 - Word (DOC)

4 - Excel

in the SelectedIndexChanged of the Drop down list I call the function to export the report

NOTE (Namespaces to include)

Do remember to include the proper namespaces, for both the memory references and the Crystal reports to load and run

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;

CODE

protected void ddlExportTypes_SelectedIndexChanged(object sender, EventArgs e)
{

ExportReport();

}

//And here's the ExportReport function

private void ExportReport()

{

//declare a memorystream object that will hold out output
MemoryStream oStream;

//here's the instance of a valid report, one which we have already Load(ed)

crReport= new ReportDocument();

/**remember that a valid crystal report has to be loaded before you run this code**/

//clear the response and set Buffer to true

Response.Clear();

Response.Buffer = true;

switch(ddlExportTypes.SelectedItem.Value)

{

case "1":

// ...Rich Text (RTF)

oStream = (MemoryStream)crReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.RichText);

Response.ContentType = "application/rtf";

break;

case "2":

// ...Portable Document (PDF)


oStream = (MemoryStream)crReport.ExportToStream(ExportFormatType.PortableDocFormat);

Response.ContentType = "application/pdf";

//in case you want to export it as an attachment use the line below

/*

crReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Your Exported File Name");

* */


break;

case "3":

// ...MS Word (DOC)

oStream = (MemoryStream)crReport.ExportToStream(ExportFormatType.WordForWindows);

Response.ContentType = "application/doc";

break;

case "4":

// ...MS Excel (XLS)

oStream = (MemoryStream)crReport.ExportToStream(ExportFormatType.Excel);

Response.ContentType = "application/vnd.ms-excel";

break;

default:

//...Portable Document (PDF)

oStream = (MemoryStream)crReport.ExportToStream(ExportFormatType.PortableDocFormat);

Response.ContentType = "application/pdf";

break;

}


try

{

//write report to the Response stream

Response.BinaryWrite(oStream.ToArray());

Response.End();

}

catch (Exception ex)

{

labelErrors.Text = "ERROR: " + Server.HtmlEncode(ex.Message.ToString());

}

finally

{

//clear stream

oStream.Flush();

oStream.Close();

oStream.Dispose();

}

}

NOTE:

There is a processing limit that the report engine can handle. The default is 75. This may be too low for many scenarios. In this case you have two options:

1) Change the registry in Windows to allow for more reports to be printed (Careful editing registry values and do not overdo the increase, as more memory will be required to run the report)

HKEY_LOCAL_MACHINE > SOFTWARE > Crystal Decisions > Report Application Server > InprocServer > find PrintJobLimit and change the value from 75 to the value you want

2) Close the report on your code page (the preferred way, but doing this with the above step will make the whole reporting experience a little better) in Page_Unload. For example:

protected void Page_Unload(object sender, EventArgs e)

{

crReport.Close();

}

This will load the report, print it and close it. You can close the report in other places within your code, but be careful where you close it. Read more from MS

http://msdn2.microsoft.com/en-us/library/ms225490(VS.80).aspx

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


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

Comments and Discussions

 
Questionerror on path load Pin
vishalbor13-Nov-13 0:25
vishalbor13-Nov-13 0:25 
GeneralMy vote of 2 Pin
Anurag Sarkar23-Jul-12 23:07
Anurag Sarkar23-Jul-12 23:07 
Questionthanks Pin
John Baos15-May-12 22:49
John Baos15-May-12 22:49 
Generalexport crystal reports and loader later Pin
virtualKing0930-Apr-10 6:00
virtualKing0930-Apr-10 6:00 
Questiona request from hkdhamija Pin
Sean Ewington19-Jul-07 4:42
staffSean Ewington19-Jul-07 4:42 
QuestionLoad RTF Pin
KillerTiger8-May-07 10:28
KillerTiger8-May-07 10:28 
AnswerRe: Load RTF Pin
LeoSimon11-May-07 7:39
LeoSimon11-May-07 7:39 
GeneralThank you Pin
RyanHerb25-Mar-07 14:29
RyanHerb25-Mar-07 14:29 
GeneralRe: Thank you Pin
LeoSimon26-Mar-07 16:46
LeoSimon26-Mar-07 16:46 
GeneralYou may want to consolidate this into a few lines Pin
ednrgc9-Mar-07 5:17
ednrgc9-Mar-07 5:17 
GeneralRe: You may want to consolidate this into a few lines Pin
LeoSimon9-Mar-07 6:22
LeoSimon9-Mar-07 6:22 

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.