Sunday, April 29, 2012

How to export a Crystal Report using C#

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 = new MemoryStream(); // using System.IO

//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

No comments:

Post a Comment