SIGN UP MEMBER LOGIN:    
ARTICLE

How to Export Crystal Report in ASP.NET

Posted by chris wong Articles | Crystal Reports C# May 06, 2004
When using Crystal Reports in ASP.NET, the CrystalReportViewer control does not have the export or the print buttons like the one in Windows Form. We can still achieve some degree of export and print functionality by writing our own code to handle exporting.
Reader Level:

When using Crystal Reports in ASP.NET in a Web Form, the CrystalReportViewer control does not have the export or the print buttons like the one in Windows Form. We can still achieve some degree of export and print functionality by writing our own code to handle exporting. If we export to PDF format, Acrobat can handle the printing for us, as well as saving a copy of the report.

First, design your Web form, add the Crystal Report Viewer and a button called "Export". I assume a new crystal report has been created and it's called report.

ExportCrystalReport1.jpg

So your code will probably look like this now

public class WebForm1 : System.Web.UI.Page
{
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
protected System.Web.UI.WebControls.Button Button1;
private CrystalReport1 report = new CrystalReport1();
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
CrystalReportViewer1.ReportSource = report;
CrystalReportViewer1.Visible =
true;
}
......

Now, all we need to do is to add the event handler for the export button and the code should look like this.

private void Button1_Click(object sender, System.EventArgs e)
{
MemoryStream oStream;
// using System.IO
oStream = (MemoryStream)
report.ExportToStream(
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.Buffer=
true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(oStream.ToArray());
Response.End();
}

That's all there is. One good thing about this is that no files are written on the server side. The available formats are PDF, Winword and Excel, but I think PDF is probably the most useful. You can also add code to dispose the memory stream and do a garbage collection if you want to ensure that resources are optimally managed.

ExportCrystalReport2.jpg

That's a sample report on our simple web page

ExportCrystalReport3.jpg

That's what happens when we click the export button.

Login to add your contents and source code to this article
share this article :
post comment
 

I need complete reference for report.

Posted by Biswarup Ghosh Apr 26, 2012

Many Many Thanks But one problem if i want to show a new page that PDF report, what should i can fo

Posted by Nasir Uddin Dec 28, 2011

Do you know if this is possible?

Posted by Todd Aug 19, 2011

can u pls explain how to send this exported as an attachment during runtime...we are not saving this pdf anywhere

Posted by meenu praveen May 03, 2011

good one. using this , is way to save to server side. because why i have to send mail as a attachment any idea any idea to save this in server side

Posted by veeramani narayanasamy Jan 07, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor