Export Crystal Report in Various Formats Using ASP.Net

This article shows how to save a Crystal Reports data in PDF, Word and Excel formats with an ASP.NET button control.

The following is the procedure.

STEP 1

DATABASE SIDE

First create a table in a SQL database for Employee Information.

  1. Create an Employee Table as in the following.
    1. create table EmployeeInformation   
    2. (  
    3. EmpId int identity(1, 1) Primary Key,  
    4. EmpName varchar(20),  
    5. EmpContact nchar(15),  
    6. EmpDeparatment varchar(30),  
    7. EmpCity varchar(30)  
    8. )  
    In this table EmpId is an auto-increment field for Employee identity.

  2. Create Procedure

    Now create a select Stored Procedure for Employee information as in the following:
    1. Create procedure sp_EmployeeInformation  
    2. As  
    3. Begin  
    4. Set nocount on;  
    5. Select EmpName,EmpContact,EmpDeparatment,EmpCity   
    6. From EmployeeInformation  
    7. End  
  3. Insert some employee records into the table as shown below:
    1. insert into EmployeeInformation values('Jonny','9988774445','IT','Delhi')  
    2. insert into EmployeeInformation values('Karan','9987534445','EC','Pune')  
    3. insert into EmployeeInformation values('Mitali','9988774741','HR','Kolkata')  
    4. insert into EmployeeInformation values('Rohan','8888774445','IT','Jaipur')  
    5. insert into EmployeeInformation values('Sapnil','9988714785','HR','Gurgaon')  
    6. insert into EmployeeInformation values('Sonika','9989994445','ELC','Bangalore')  
    7. insert into EmployeeInformation values('Punit','9888774445','ACC','Delhi')  
    8. insert into EmployeeInformation values('Ketan','9988774445','IT','Chennai')  
    9. insert into EmployeeInformation values('Jennit','9986541235','EC','Delhi')  
    10. insert into EmployeeInformation values('Kavita','7845129635','ELC','Madurai')  

STEP 2

OPEN PROJECT IN VS


STEP 3

Go to the Solution Explorer as in the following and add a new item as in Figure 1.



Figure 1: Add New Item

Add Crystal Report
Figure 2: Add Crystal Report

Report Gallery

Figure 3: Select Report Gallery

ADO Connection

Figure 4: ADO Connection

Select SQL Server Native Client

Figure 5: Select SQL Server Native Client

Provide Database Connection Information

Figure 6: Provide Database Connection Information

Connection Information

Figure 7: Connection Information

Select Created Procedure

Figure 8: Select Created Procedure

Choose Field

Figure 9: Choose Field

Choose Report Display Format

Figure 10: Choose Report Display Format

Finally your Design Page is ready as in the following figure,

Report Design Format

Figure 11: Report Design Format

STEP 4

Go to Solution Explorer and add a new item.

Add UI

Figure 11: Add UI

STEP 5

Add a Report Viewer.

Add Crystal ReportViewer

Figure 12: Add Crystal ReportViewer

Step 6

UI Design Side

Now we are adding a button control to the .aspx page and create a button control event.

Then write report data export code for this new button event.

You can see the design code below:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="ImployeeInformation.aspx.cs" Inherits="Test_WebApplication.ImployeeInformation" %>  
  2. <%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">  
  6. <div>  
  7. <h2>Report Data Export In PDF, Word and Excel Document </h2>  
  8. <table style="width: 700px">  
  9. <tr>  
  10. <td>Save Report Data In Different  Format</td>  
  11. <td><asp:Button ID="btnPDF" runat="server" Text="PDF Format" onclick="btnPDF_Click" /></td>  
  12. <td><asp:Button ID="btnExcel" runat="server" Text="EXCEL Format" onclick="btnExcel_Click"/></td>  
  13. <td><asp:Button ID="btnWord" runat="server" Text="WORD Format"   
  14. onclick="btnWord_Click" Width="129px" /></td>  
  15. </tr>  
  16. </table>  
  17. </div>  
  18. <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"   
  19. AutoDataBind="true" />  
  20. </asp:Content>   
STEP 7

UI CODE SIDE

In this section the report load code is written in the page load event. So the report data displays in the browser when you run the report. Then we will write code for record exporting into a different format and save all the records. You can see the code below:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using CrystalDecisions.CrystalReports.Engine;  
  8. using CrystalDecisions.Shared;  
  9.   
  10. namespace Test_WebApplication {  
  11.     public partial class ImployeeInformation: System.Web.UI.Page   
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)   
  14.         {  
  15.             ReportDocument Report = new ReportDocument();  
  16.             Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));  
  17.             Report.SetDatabaseLogon("sa"" password ""Rakesh-PC""SqlServerTech");  
  18.             CrystalReportViewer1.ReportSource = Report;  
  19.         }  
  20.   
  21.         protected void btnPDF_Click(object sender, EventArgs e)   
  22.         {  
  23.             ReportDocument Report = new ReportDocument();  
  24.             Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));  
  25.             Report.SetDatabaseLogon("sa""password ""Rakesh-PC""SqlServerTech");  
  26.             Response.Buffer = false;  
  27.             Response.ClearContent();  
  28.             Response.ClearHeaders();  
  29.             Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true"EmployeeInformation");  
  30.             Response.End();  
  31.         }  
  32.   
  33.         protected void btnExcel_Click(object sender, EventArgs e)   
  34.         {  
  35.             ReportDocument Report = new ReportDocument();  
  36.             Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));  
  37.             Report.SetDatabaseLogon("sa""password""Rakesh-PC""SqlServerTech");  
  38.             Response.Buffer = false;  
  39.             Response.ClearContent();  
  40.             Response.ClearHeaders();  
  41.             Report.ExportToHttpResponse(ExportFormatType.Excel, Response, true"EmployeeInformation");  
  42.             Response.End();  
  43.         }  
  44.   
  45.         protected void btnWord_Click(object sender, EventArgs e)   
  46.         {  
  47.             ReportDocument Report = new ReportDocument();  
  48.             Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));  
  49.             Report.SetDatabaseLogon("sa"" password ""Rakesh-PC""SqlServerTech");  
  50.             Response.Buffer = false;  
  51.             Response.ClearContent();  
  52.             Response.ClearHeaders();  
  53.             Report.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true"EmployeeInformation");  
  54.             Response.End();  
  55.         } 
  56.     }  
  57. }  
STEP 8

BROWSER SIDE

Now you can run the report in the browser and see the following:

Report Display

Figure 12: Report Display

Now open all the format files and check that the report data is in various formats as shown in Figure 13.



Figure 13: Report Export in Word Format



Figure 14: Report Export in Excel Format



Figure 15: Report Export in PDF Format

Note: maintain your database connection string in your project web.config file.

Up Next
    Ebook Download
    View all
    Learn
    View all