Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Crystal Reports » 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.

Total page views :  213328
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

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.


Login to add your contents and source code to 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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
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.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Click Here for 6 Months Free! Powerful ASP.NET Hosting at your Fingertips!
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 | Delete | Modify | 
Re: excellent article by Le Khac Chinh On August 22, 2009
GREATTTTTTTTTTTTTT WORKKKKKKKKKKKK! IT SAVES MY TIME VERY MUCH!!!
Reply | Email | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | Modify | 
Xcelent!!! by naushad On March 14, 2007
Its realy excellent article...thanx chris.
Reply | Email | Delete | 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 | Delete | 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 | Delete | 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 | Delete | Modify | 
Re: Re: diffrent problem by sunil On August 17, 2007

Hi,

I am too new to crystal reports & facing the same problem. If you got the solution then pass it to me.

 

sunilk.net@gmail.com

 

 

sunil...

Reply | Email | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | Modify | 
How to Export Crystal Report in ASP.NET by Takaendesa On November 7, 2008
Man this' excellent!!
Reply | Email | Delete | 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 | Delete | 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 | Delete | 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 | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.