Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Crystal Reports C# » How to Export Crystal Report in ASP.NET

How to Export Crystal Report in ASP.NET

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.

Page Views : 277203
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.



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.

That's a sample report on our simple web page

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

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
chris wong
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
excellent article by serge On May 3, 2006
exxcelent article
 
i just added this code at the end of my viewer and it now print in PDF instead of html
 
Very nice, considering a lot of poeple search for that on the net
 
What i did till now:
report in sql in crystal web form
report in csv in crystal web form
both in pdf now :) thx to you
 
Serge Fournier
Programmer
Reply | Email | Modify 
Re: excellent article by Le Khac Chinh On August 22, 2009
GREATTTTTTTTTTTTTT WORKKKKKKKKKKKK! IT SAVES MY TIME VERY MUCH!!!
Reply | Email | Modify 
doesn't work by peachmuiker On June 16, 2006
i tryed it but doesn't work. it just show blank page.
 
what's the matter wrong
 
can help me?
 
peachmuiker
 
Reply | Email | Modify 
Re: doesn't work by Mahesh On June 13, 2007
Check your data by debugging in the code. Its possible your report is not getting any data.
Reply | Email | Modify 
Converting Crystal Report to PDF by Gregory On February 5, 2007
My crystal report displayed a list of values, which would fit on 2 pages, if I printed it; However, after converting the report to PDF, each record got its own page in the generated PDF document... What can be done to correct this issue? I used the code from the article to convert the report to PDF format.
Reply | Email | Modify 
Re: Converting Crystal Report to PDF by Mahesh On June 13, 2007
You may want to select "Suppress blank spaces" on each section of your report.
Reply | Email | Modify 
call Crystal report 11 in ASP.NET by roni On December 28, 2007

Mr. maybe you can help me how I call crystal report 11 develover in ASP.Net. I Hope you can help me, because this problem is very long time i can't finish my job.

 

Reply | Email | Modify 
SubReport Question by x On February 8, 2007
I've used this code for a while and it works well on a single crystal report. What about reports with subreports? I tried this on a report I have that includes a subreport and I get a massive login error. The report and subreport work fine in a crystal viewer but the export script fails on the subreport. Whay? How can I modify the export code to allow for reports with subreports?
Reply | Email | Modify 
its realy help me by asghar On March 10, 2007
hi chris wong this is fantastic article I used this code and it solved my problem. But I also want to convert crystal report into MS Word i replace msword inplace of pdf it open word file but format is garbage collection. plz help to convert report inot word format. Thanks
Reply | Email | Modify 
crystal report conversion into Excel by asghar On March 10, 2007

Hi Chris wong I want to convert crystal report into Excel format using your code writen bellow but i am not able to do that plz give help. thanks.

 

MemoryStream oStream; // using System.IO

oStream = (MemoryStream)

report.ExportToStream(

CrystalDecisions.Shared.ExportFormatType.ExcelRecord);

Response.Clear();

Response.Buffer = true;

Response.ContentType = "application/msexcel";

Response.BinaryWrite(oStream.ToArray());

Response.End();

Reply | Email | Modify 
Re: crystal report conversion into Excel by threekhanz On October 24, 2007

Hello,

I think Export Format Type should be
oStream = rd.ExportToStream(ExportFormatType.Excel)
and
Content Type should be like this,
Response.ContentType = "application/vnd.ms-excel"

Now, hope it will work fine........... cheers :)

Reply | Email | Modify 
Xcelent!!! by naushad On March 14, 2007
Its realy excellent article...thanx chris.
Reply | Email | Modify 
problem to export to pdf from crystal viewer by shekhar On July 9, 2007

hi chris, its a excelent articke no doubt in that, but i have some problem in that. In my web page i am selecting a value and depend up on that i am getting report in crystal viewer. Till here every thing is fine but when i am exporting that to pdf format using ur code, its showing some particular record every time instead what i have selected in my web page. here are the code

string pi_code=ddlpi_number.SelectedItem.ToString () ;//select Proforma invioice code from dropdown list

report.Load(Server.MapPath("proforma_invoice.rpt"));

report.SetParameterValue ("pi_code",pi_code); // passing that PI number to report

report.SetDatabaseLogon("sa","sa"); //report is a reportdocument

ConfigureCrystalReports();//login to all tables

CrystalReportViewer1.ReportSource =report;

then i used ur code to another button click

MemoryStream oStream; // using System.IO

oStream = (MemoryStream)pi_report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); //here pi_report is my crystal report object

Response.Clear();

Response.Buffer= true;

Response.ContentType = "application/pdf";

Response.BinaryWrite(oStream.ToArray());

Response.End();

can u please help me?

thanks in advance.

shekhar roy

Reply | Email | Modify 
diffrent problem by ajay On July 13, 2007
hello this is very good article but i have some other problem .i want to show report on bases of some parameter.i want that i paas any ID OR Name and want to show record releted to this record on another web page.so please can u tell me what is the solution of this problem...my email id ajayrathi2004@yahoo.com
Reply | Email | Modify 
Re: diffrent problem by gokou On August 12, 2007
good day i have tried your codes but when im trying to test it an error accured " The type or namespace 'CrystalReports1' could not be found(are youe missing a using directive or an assembly reference?) please help me to correct this error im only new on using crystal reports that why i dong have any idea to correct this. thanks..
Reply | Email | Modify 
Re: Re: diffrent problem by sunil On August 17, 2007
<P>Hi,</P> <P>I am too new to crystal reports & facing the same problem. If you got the solution then pass it to me.</P> <P> </P> <P> </P> <P> </P> <P>sunil...</P>
Reply | Email | Modify 
Re: Re: Re: diffrent problem by shaalini On September 13, 2007
hi, i am new to crystal report. I am facing the same problem as namespace expected
Reply | Email | Modify 
Re: Re: Re: Re: diffrent problem by Moutasem On September 26, 2007

HI MR.SHAALINI,

I WANT ASK YOU FROM THE FIRST STEP HOW CAN I CREATE REPORT.

PLEASE HELP ME.

BEST REGARDS

MOUTASEM

Reply | Email | Modify 
Re: Solution by Senthil B On January 21, 2010
Try to add Crystal Report on your References and then use the namespace to continue.
Reply | Email | Modify 
Error by Ankur On October 1, 2007
Error 1 'CrystalDecisions.Web.CrystalReportSource' does not contain a definition for 'ExportToStream' C:\Documents and Settings\Ankur Kathuria\My Documents\Visual Studio 2005\WebSites\EnfisReportTest\report.aspx.cs 21 40 C:\...\EnfisReportTest\
Reply | Email | Modify 
It did not work; Database log in error by Aur On December 7, 2007
I got a log on error. Please help ---------------------------------- CrystalDecisions.CrystalReports.Engine.LogOnException: Database logon failed. ---> System.Runtime.InteropServices.COMException (0x8004100F): Database logon failed. at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext) at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext) ---
Reply | Email | Modify 
It did not work; Database log in error by Aur On December 7, 2007
I got a log on error. Please help ---------------------------------- CrystalDecisions.CrystalReports.Engine.LogOnException: Database logon failed. ---> System.Runtime.InteropServices.COMException (0x8004100F): Database logon failed. at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext) at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext) ---
Reply | Email | Modify 
Crystal report export in pdf Format does not work in page_load() event by Aminul On December 12, 2007
Hi all, I'm new in asp.net. can any one help me please how can i use crystal report export in pdf format in page_load() event. i can use the following code for any button event but it doesn't work in page_load() event. please help me.. 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();
Reply | Email | Modify 
Crystal report export in pdf Format does not work in page_load() event by Aminul On December 12, 2007
Hi all, I'm new in asp.net. can any one help me please how can i use crystal report export in pdf format in page_load() event. i can use the following code for any button event but it doesn't work in page_load() event. please help me.. 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();
Reply | Email | Modify 
Optimisation required for exporting data by abhishek On December 12, 2007
HI !! I have write the same code as u have shown in the article but my problem is not that because when there are small no of data then it works perfect but when i try more then 2000 data at same time it takes too much time. so if u can suggest some code to optimize teh coding u have wrtie then it will be verry useful. I think all of us will get the same problem once they try large data. Thanks in Advance, Abhi
Reply | Email | Modify 
Export to excel with parameter by shms On March 3, 2008

Helloo ... How do i export a report with parameters ? how do i specify the parameters to the report document plzz help. When i export to excel the report is gives me an error mentioning "Missing Parameter field current value" Please Help

thanks in Advance

Reply | Email | Modify 
error message by noora On March 17, 2008
Hi, than you for your great articals but i got an error message as follows:: Specified argument was out of the range of valid values. Parameter name: offset how can i solve this
Reply | Email | Modify 
Error on Production system by Larry On March 20, 2008
Hi, Excellent article. The problem I'm having is that on my development and testing servers, exporting CR to pdf and Excel works fine. However, on the production server I can only export to pdf. When I export to Excel I get the following error: Error detected by export DLL: at .F(String  , EngineExceptionErrorID  ) at .A(Int16 , Int32 ) at .@(Int16 ) at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext) at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportFormatType formatType) at mats.rptRequestHistoryByUser1.cmdExportToExcel_Click(Object sender, EventArgs e) in [application].aspx.vb:line 127 where line 127 is the Export line. I've checked the dll's and the GAC on all 3 machines and everything looks the same. Word works fine too. I know it's not CR as the export to pdf works, and also another Excel function has the same error. Has anyone seen this before? Thanks! Larry
Reply | Email | Modify 
Points Wrong in the Article by Roopangee On September 19, 2008
Hello sir, I am sorry to write that it is not possible to create an instance of crystal report in asp.net application as is shown above.I had tried hard several times and it is not working as written below private CrystalReport1 report = new CrystalReport1(); .Kindly reply. Thanking you, Roopangee
Reply | Email | Modify 
Error Specified argument is out of range of valid values. Parameter name Value by Jhanvy On October 15, 2008
I got this error while opening Crystal report forms in ASP.NET 2003 deployed project. Crystal Report with only one or two fields opens well but report with parameters and formulae displays error Specified argument is out of range og valid values.Parameter name Value
Reply | Email | Modify 
How to Export Crystal Report in ASP.NET by Taka On November 7, 2008
Man this' excellent!!
Reply | Email | Modify 
Problem in Converting Crystal Report Into Word by khurram On July 6, 2009
Following Error is occuring,when i tried to convert report int word.(Please guide me what should i do)

Error:

The XML page cannot be displayed

Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


Invalid at the top level of the document. Error processing resource 'http://localhost/NewWebRpt/Frm_Event.aspx'. Line 1, P...

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fswiss\fcharset0 Century Gothic;}}{\colo...

Thanks

 

Reply | Email | Modify 
Re: Problem in Converting Crystal Report Into Word by Nishant On January 4, 2011
Dear Friend i am getting same problem whenever i am tring to convert my crystal report into word.please solve out my prblem as soon as possible.my personal id is nishugimt@gmail.com
Reply | Email | Modify 
Need Help by Luis On September 25, 2009
Hello Chris, I never use C, just vb6 and small part VB.net
Is possible share the source code?
I need view my CR8 report in WEB.
BR
Reply | Email | Modify 
Crystal Report is not used here by anto On February 4, 2010
hi chris,

The above coding you are using only the http Response Object to display the pdf.

Crystal Report is doing nothing here.

Regards,
Reply | Email | Modify 
crystal report by Sourabh On December 11, 2010
check this link how to make crystal report http://www.mindstick.com/Beginner/BeginnerData.aspx?aid=032E6628-EBEA-480B-B784-8126FC533758%20&t1=Reporting%20&t2=CrystalReportViewer%20Control%20in%20ASP.Net"
Reply | Email | Modify 
same error by Nishant On January 4, 2011
Hi Chris I am getting same problem whenever i am tring to export me crystal report into word.if u have any solution please help me....my problem is bellow as:- The XML page cannot be displayed Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later. -------------------------------------------------------------------------------- Invalid at the top level of the document. Error processing resource 'http://localhost:49229/TLILOGISTICS/Report/MealsReport... {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fcharset0 Arial;}{\f1\fmodern\fcharset0\fprq1 IDAutomationHC39...
Reply | Email | Modify 
great stuff by veeramani On January 7, 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
Reply | Email | Modify 
fgtf by sachin On February 24, 2011
gfgfgfgfgf
Reply | Email | Modify 
EMAIL THIS EXPORTED PDF AS ATTACHMENT by meenu On May 3, 2011
can u pls explain how to send this exported as an attachment during runtime...we are not saving this pdf anywhere
Reply | Email | Modify 
Exporting to Report Definition by Todd On August 19, 2011
Do you know if this is possible?
Reply | Email | Modify 
Nice by Nasir On December 28, 2011
Many Many Thanks But one problem if i want to show a new page that PDF report, what should i can fo
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.