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

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

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

Share

About the Author

LeoSimon
Web Developer
Canada Canada
No Biography provided

Comments and Discussions

 
Questionerror on path load Pinmembervishalbor13-Nov-13 0:25 
GeneralMy vote of 2 Pinmemberonurag1923-Jul-12 23:07 
Questionthanks PinmemberJohn Baos15-May-12 22:49 
Generalexport crystal reports and loader later PinmembervirtualKing0930-Apr-10 6:00 
Questiona request from hkdhamija PinadminSean Ewington19-Jul-07 4:42 
"Hi, I have use this example, but in my case it not working, its Exporting a blank file in all the format. please help me its Very urgent to me."
- hkdhamija
QuestionLoad RTF PinmemberKillerTiger8-May-07 10:28 
AnswerRe: Load RTF PinmemberLeoSelmani11-May-07 7:39 
GeneralThank you PinmemberRyanHerb25-Mar-07 14:29 
GeneralRe: Thank you PinmemberLeoSelmani26-Mar-07 16:46 
GeneralYou may want to consolidate this into a few lines Pinmemberednrgc9-Mar-07 5:17 
GeneralRe: You may want to consolidate this into a few lines PinmemberLe09-Mar-07 6:22 

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.141015.1 | Last Updated 11 May 2007
Article Copyright 2007 by LeoSimon
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid