Introduction
When using Crystal Reports in ASP.NET in a Web Form, the CrystalReportViewer control does not have the export of 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 exports. 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 of 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.

sanjay acharyaPosted Aug 8, 2013, 9:03 AM EDT
good content..............
Senthil B KumaranPosted Feb 1, 2013, 2:12 AM EST
Simply Great Work! You got the good idea to use the right thing (MemoryStream) at right scenario. Super.
Rakesh ThakrePosted Sep 24, 2012, 1:10 AM EDT
I need to all buttons on left hand side and when I generate any report it would appear on right hand side. but when I generates report and export to PDF directly it come but occupy all web page space. I just need to show in specific area. If any on.e have solutions over my problem please tell me. Thanks
Biswarup GhoshPosted Apr 26, 2012, 2:30 AM EDT
I need complete reference for report.
Nasir UddinPosted Dec 28, 2011, 3:21 AM EST
Many Many Thanks But one problem if i want to show a new page that PDF report, what should i can fo
ToddPosted Aug 19, 2011, 3:33 PM EDT
Do you know if this is possible?
meenu praveenPosted May 2, 2011, 10:45 PM EDT
can u pls explain how to send this exported as an attachment during runtime...we are not saving this pdf anywhere
veeramani narayanasamyeditedPosted Jan 6, 2011, 9:41 PM ESTEdited Jan 6, 2011, 9:44 PM EST
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
Nishant SinghPosted Jan 3, 2011, 10:18 PM EST
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...
Sourabh SachaneditedPosted Dec 11, 2010, 4:02 AM ESTEdited Dec 11, 2010, 4:07 AM EST
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"
anto justusPosted Feb 4, 2010, 1:09 AM EST
hi chris, The above coding you are using only the http Response Object to display the pdf. Crystal Report is doing nothing here. Regards,
Luis LopezPosted Sep 25, 2009, 12:45 PM EDT
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
khurram khanPosted Jul 5, 2009, 10:36 PM EDT
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
Taka ChidodaPosted Nov 7, 2008, 3:49 AM EST
Man this' excellent!!
Jhanvy ThakerPosted Oct 14, 2008, 11:56 PM EDT
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
Roopangee TandonPosted Sep 19, 2008, 5:53 AM EDT
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
Larry WakemanPosted Mar 20, 2008, 9:08 AM EDT
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
noora noPosted Mar 16, 2008, 10:10 PM EDT
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
shms sseditedPosted Mar 3, 2008, 3:45 PM ESTEdited Mar 3, 2008, 3:46 PM EST
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
abhishek trivediPosted Dec 12, 2007, 2:35 AM EST
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
Aminul IslamPosted Dec 11, 2007, 9:25 PM EST
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();
Aminul IslamPosted Dec 11, 2007, 9:21 PM EST
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();
AurPosted Dec 7, 2007, 5:31 AM EST
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) ---
AurPosted Dec 7, 2007, 5:27 AM EST
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) ---
Ankur KathuriaPosted Oct 1, 2007, 1:39 AM EDT
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\
ajay rathiPosted Jul 12, 2007, 10:09 PM EDT
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 [email protected]
shekhar royeditedPosted Jul 9, 2007, 5:34 AM EDTEdited Jul 9, 2007, 5:42 AM EDT
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
naushad aliPosted Mar 13, 2007, 10:49 PM EDT
Its realy excellent article...thanx chris.
asghar alieditedPosted Mar 10, 2007, 2:53 AM ESTEdited Mar 10, 2007, 3:00 AM EST
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();
asghar alieditedPosted Mar 10, 2007, 2:20 AM ESTEdited Mar 10, 2007, 2:24 AM EST
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
xPosted Feb 8, 2007, 6:42 AM EST
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?
GregoryPosted Feb 5, 2007, 8:58 AM EST
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.
peachmuikerPosted Jun 15, 2006, 8:26 PM EDT
i tryed it but doesn't work. it just show blank page. what's the matter wrong can help me? peachmuiker
serge fournierPosted May 3, 2006, 9:23 AM EDT
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