You are on page 1of 9

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

Search
Home >> ASP.NET Tutorials

Export Crystal Reports to PDF file

Reports are used by business for various management purposes. Crystal reports is very versatile and easy to use reporting tool that is bundled with the Microsoft Visual Studio .Net. There have been many enhancements in the latest version of crystal reports which make it simpler for developers to export reports directly to the HTTP response object.

The export option is very useful when business users want to have reports exported different formats such as PDF, Excel, Word and HTML. They want this facility in order to download the report and store it for future use or to modify the report further (possible in case of Word and Excel). In many cases the export option is used for scheduled reports to be send via email, In this case, the exporting is done on the server side and the exported file is attached to an email and send to the user's email id. In this tutorial, I will be showing you how to export a report to PDF format, but it can be easily modified to export the report in other formats. Related articles: 1. Build Notepad ASP.NET Web Application 2. Taskbar Notification with Irregular Shaped Forms 3. How To Fill DropDownList Control With ArrayList? 4. Can I Get Conditional RequiredFieldValidator On Client Side?

Create a Crystal Report and display to the user


Create a New ASP.NET Web Site Project

I will be using an XML file as the data source for this tutorial. In normal scenarios a business layer class would return a dataset or datatable containing data to be displayed in the report. So I created an XML file with customer data and will be reading the contents into a Dataset.

Add XML file to the Project


Right-Click on App_Data folder and select Add Existing Item

1 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

Browse to the XML file and select it:

Add a crystal report to the project


Right-Click on the website root and select Add New Item. Select "Crystal Report". Enter the name of the report, I used the default name given by the IDE.

When the Crystal report wizard comes up select "Standard" and click OK.

2 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

Select a data source for the report. Expand the Create New Connection node and then expand the ADO.net option.

Browse to the XML file and select the file.

The Customers table will appear in the list of data sources, add the table to selected table list by clicking on the right arrow.

3 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

Select all the columns that you need to display in the reports. I selected all of them for simplicity.

Setup grouping as needed to display the report.

Setup fields available for filtering.

4 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

Select the layout of report that you want.

The report is now created, you can now change the layout, add logos, etc.

Display the report on a web page


In the design view drag and drop the crystal reports viewer from Toolbox onto the web page. Add a button which will handle the displaying of the report. The HTML for body of the webpage should like as follows: <body> <form id="form2" runat="server"> <div> <asp:Button ID="btnShow" runat="server" OnClick="btnShow_Click" Text="Show Report in Viewer" /><br />

5 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

<br /> <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" /> </div> </form> </body> Remember to add following namespaces at the top of code behind page for referencing Crystal Report objects. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; The event handler should looks as follows: protected void btnShow_Click(object sender, EventArgs e) { // Get the report document and bind it with the crystal report viewer CrystalReportViewer1.ReportSource = getReportDocument(); CrystalReportViewer1.DataBind(); } // Returns a ReportDocument object with the report and data loaded private ReportDocument getReportDocument() { // File Path for Crystal Report string repFilePath = Server.MapPath("CrystalReport.rpt") // Declare a new Crystal Report Document object // and the report file into the report document ReportDocument repDoc = new ReportDocument(); repDoc.Load(repFilePath); // Set the datasource by getting the dataset from business // layer and // In our case business layer is getCustomerData function repDoc.SetDataSource(getCustomerData()); } // Business layer class to get the data from database private DataSet getCustomerData() { string customerFilePath = Server.MapPath("App_Data\\Customers.xml"); DataSet ds = new DataSet(); ds.ReadXml(customerFilePath); }

Run the Project to see if if works.

Export the Crystal Report in PDF format


Add a button to the web page so that user can click and get the PDF.

6 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

The HTML for web page should looks as follows: <body> <form id="form2" runat="server"> <div> <asp:Button ID="btnShow" runat="server" OnClick="btnShow_Click" Text="Show Report in Viewer" /><br /> <br /> <asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export To PDF" /> <br /> <br /> <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" /> </div> </form> </body> The Code behind for export is as follows: protected void btnExport_Click(object sender, EventArgs e) { // Get the report document ReportDocument repDoc = getReportDocument(); // Stop buffering the response Response.Buffer = false; // Clear the response content and headers Response.ClearContent(); Response.ClearHeaders(); try { // Export the Report to Response stream in PDF format and file name Customers repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat,Response,true,"Customers"); // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports } catch(Exception ex) { Console.WriteLine(ex.Message); ex = null; } }

Export to other formats


The report can be exported in different formats as follow: PDF ExportFormatType.PortableDocFormat

Complete Report to ExportFormatType.Excel Excel Only Report Data to Excel HTML RTF Word ExportFormatType.ExcelRecord ExportFormatType.HTML40 ExportFormatType.RichText ExportFormatType.WordForWindows

Run the project to see does it working.

7 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

Once you click on the Export the PDF button the dialog box to download the pdf file will appear.

Now open the file and see if it is displayed properly.

By the end if this tutorial you should be able to 1) Create simple crystal reports 2) Display it to a web page using a crystal reports viewer 3) Allow users to download the reports in various formats such as PDF, Excel, Word and HTML. You can download Sample Export Crystal Report To PDF Visual Studio Project, used in this tutorial. Many people found that Crystal Reports are complicate to learn. Personally, I like Stimulsoft Reports for ASP.NET application. If you use SQL Server, nice solution is ApexSQL Report. This tutorial is written by Chirag Nirmal - CANSoft.

Latest tutorials:
How I Found Reliable ASP.NET Web Hosting My story from web hosting troubles to reliable ASP.NET web hosting company. .Net Regular Expressions Quantifiers Explains how to write regular expressions

8 de 9

12/06/12 16:52

Export Crystal Reports to PDF le

http://www.beansoftware.com/ASP.NET-Tutorials/...

quantifiers in .Net Framework RegEx engine. How To Use Replace function in TEXT and NTEXT fields Explains how to use T-SQL to search and replace text in TEXT or NTEXT fields.

Tutorial toolbar: Tell A Friend | Add to favorites | Feedback

Copyright 2002 - 2012 Bean Software. All rights reserved.

9 de 9

12/06/12 16:52

You might also like