Click here to Skip to main content
11,928,352 members (49,451 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked

How to export a Crystal Report using C#

, 11 May 2007
Rate this:
Please Sign up or sign in to vote.
This article describes how to use C# to customize exporting a Crystal Report


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 ex port 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;


protected void ddlExportTypes_SelectedIndexChanged(object sender, EventArgs e)



//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.Buffer = true;



case "1":

// ...Rich Text (RTF)

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

Response.ContentType = "application/rtf";


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");

* */


case "3":

// ...MS Word (DOC)

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

Response.ContentType = "application/doc";


case "4":

// ...MS Excel (XLS)

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

Response.ContentType = "application/";



//...Portable Document (PDF)

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

Response.ContentType = "application/pdf";





//write report to the Response stream




catch (Exception ex)


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




//clear stream







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)




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


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

Web Developer
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

Questionerror on path load Pin
vishalbor13-Nov-13 1:25
membervishalbor13-Nov-13 1:25 
GeneralMy vote of 2 Pin
onurag1924-Jul-12 0:07
memberonurag1924-Jul-12 0:07 
Questionthanks Pin
John Baos15-May-12 23:49
memberJohn Baos15-May-12 23:49 
Generalexport crystal reports and loader later Pin
virtualKing0930-Apr-10 7:00
membervirtualKing0930-Apr-10 7:00 
Questiona request from hkdhamija Pin
Sean Ewington19-Jul-07 5:42
adminSean Ewington19-Jul-07 5:42 
QuestionLoad RTF Pin
KillerTiger8-May-07 11:28
memberKillerTiger8-May-07 11:28 
AnswerRe: Load RTF Pin
LeoSelmani11-May-07 8:39
memberLeoSelmani11-May-07 8:39 
GeneralThank you Pin
RyanHerb25-Mar-07 15:29
memberRyanHerb25-Mar-07 15:29 
GeneralRe: Thank you Pin
LeoSelmani26-Mar-07 17:46
memberLeoSelmani26-Mar-07 17:46 
GeneralYou may want to consolidate this into a few lines Pin
ednrgc9-Mar-07 6:17
memberednrgc9-Mar-07 6:17 
You may want to consolidate this into a few lines. There is no reason for this to be so verbose. You have a ton of redundant code in each case item.

You may also want to check into the factory pattern, and create the item based on its type. I think this may be a perfect candidate for it.

GeneralRe: You may want to consolidate this into a few lines Pin
Le09-Mar-07 7:22
memberLe09-Mar-07 7: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 11 May 2007
Article Copyright 2007 by LeoSimon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid