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, 1:03 PM
good content..............
Senthil B KumaranPosted Feb 1, 2013, 7:12 AM
Simply Great Work! You got the good idea to use the right thing (MemoryStream) at right scenario. Super.
Rakesh ThakrePosted Sep 24, 2012, 5:10 AM
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, 6:30 AM
I need complete reference for report.
Nasir UddinPosted Dec 28, 2011, 8:21 AM
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, 7:33 PM
Do you know if this is possible?
meenu praveenPosted May 3, 2011, 2:45 AM
can u pls explain how to send this exported as an attachment during runtime...we are not saving this pdf anywhere
veeramani narayanasamyeditedPosted Jan 7, 2011, 2:41 AMEdited Jan 7, 2011, 2:44 AM
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 4, 2011, 3:18 AM
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, 9:02 AMEdited Dec 11, 2010, 9:07 AM
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, 6:09 AM
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, 4:45 PM
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 6, 2009, 2:36 AM
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, 8:49 AM
Man this' excellent!!
Jhanvy ThakerPosted Oct 15, 2008, 3:56 AM
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, 9:53 AM
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, 1:08 PM
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 17, 2008, 2:10 AM
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, 8:45 PMEdited Mar 3, 2008, 8:46 PM
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, 7:35 AM
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 12, 2007, 2:25 AM
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 12, 2007, 2:21 AM
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, 10:31 AM
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, 10:27 AM
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, 5:39 AM
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 13, 2007, 2:09 AM
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, 9:34 AMEdited Jul 9, 2007, 9:42 AM
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 14, 2007, 2:49 AM
Its realy excellent article...thanx chris.
asghar alieditedPosted Mar 10, 2007, 7:53 AMEdited Mar 10, 2007, 8:00 AM
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, 7:20 AMEdited Mar 10, 2007, 7:24 AM
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, 11:42 AM
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, 1:58 PM
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 16, 2006, 12:26 AM
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, 1:23 PM
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