Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Export Crystal Report to PDF, Excel, Word in ASP.NET

Article:
Viewed:  6428 
Posted On:  19/09/2014 22:10:42 
How we can export our crystal report to pdf, excel, word in asp.net c# ? 

 

In this article I am going to explain how we can export a crystal report to PDF, EXCEL,  Word.

Here in this article I am going to display a Crystal Report by fetching records from more than one table in ASP.NET.

Below 3 tables I am using to create a Crystal Report.


1. Customer


Customer.png

Image 1.

2. Product

Product.png

Image 2.

3. Cust_Prod_Order

Customer Product Order.png

Image 3.

Now Open Visual Studio -> File -> New Web Site

2.png

Image 4.

After this Right Click on Solution Explorer -> Add New Item -> Crystal Report -> Add.

3.png

Image 5.

4.png

Image 6.

Here Expand -> Create New Connection -> Select OLE DB(ADO) -> A pop up window will open ->Select Microsoft OLE DB Provider for SQL Server -> Next

5.png

Image 7.

Now Enter your SQL server Details.

6.png

Image 8.

7.png

Image 9.

Now Select your Data Base -> Select Your All Tables and Move

8.png

Image 10.

Now You can see your tables with relationship.

9.png

Image 11.

Now select column to show in reports.

10.png

Image 12.

Now you can see your report is ready. All Columns already in Details sections. You can remove any column or you can add new column by drag n drop from field explorer to report. Here I did some formatting like header text background, detail column colour etc.

11.png

Image 13.

Now time to add a Report Viewer where we can show this Crystal Report. On Default.aspx page drag n drop CrystalReportViewer from tool box like below.

13.png

Image 14.

My aspx code is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">   
 
<title>Crystal Report From More Than One Table</title>
</head>
<body>
    <form id="form1" runat="server">
    <table cellpadding="10" cellspacing="10" width="70%" height="300px" align="center"
        style="border: solid 2px gray;">
        <tr>
            <td align="center" style="background-color: SkyBlue;">
                <span style="font-family: Times New Roman; font-size: 18pt; color: Green;">Customer
                    Product Order Detail Report</span>
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:Panel ID="pnlReport" runat="server" Height="400px">
                    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
                </asp:Panel>
            </td>
        </tr>
       <tr style="background-color: skyblue;">
            <td align="center">
               
<asp:Button ID="btnExportToPDF" runat="server" Text="Export To PDF" OnClick="btnExportToPDF_Click" />
            </td>
            <td align="center">
                <asp:Button ID="btnExportToExcel" runat="server" Text="Export To Excel" OnClick="btnExportToExcel_Click" />
            </td>
            <td align="center">
                <asp:Button ID="btnExportToWord" runat="server" Text="Export To Word" OnClick="btnExportToWord_Click" />
            </td>             
        </tr>
    </table>
    </form>
</body>
</html> 

Now on Page_Load event write below code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument cryRpt = new ReportDocument();
        cryRpt.Load(Server.MapPath("EmployeeCrystalReport.rpt"));
        CrystalReportViewer1.ReportSource = cryRpt;        
    }

    protected
void btnExportToPDF_Click(object sender, EventArgs e)
    {
        ReportDocument cryRpt = new ReportDocument();
        cryRpt.Load(Server.MapPath("CrystalReport.rpt"));
        CrystalReportViewer1.ReportSource = cryRpt;
 
        cryRpt.ExportToHttpResponse
        (CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "CustomerProOrderDetails");
    }
 
    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        ReportDocument cryRpt = new ReportDocument();
        cryRpt.Load(Server.MapPath("CrystalReport.rpt"))

        
CrystalReportViewer1.ReportSource = cryRpt;
        cryRpt.ExportToHttpResponse
             (CrystalDecisions.Shared.ExportFormatType.ExcelRecord, Response, true, "CustomerProOrderDetails");
    }
 
    protected void btnExportToWord_Click(object sender, EventArgs e)
    {
      ReportDocument cryRpt = new ReportDocument();
      cryRpt.Load(Server.MapPath("CrystalReport.rpt"));
        CrystalReportViewer1.ReportSource = cryRpt;
 
        cryRpt.ExportToHttpResponse
             (CrystalDecisions.Shared.ExportFormatType.WordForWindows, Response, true, "CustomerProOrderDetails");
    }
}
 
Now Run your Application:

12.png

Image 15.

Now click on Export To PDF Button:

14.png

Image 16.

Click on Export to Excel button:

15.png

Image 17.

16.png

Image 18.

Click on Export to Word:

17.png

Image 19.

18.png

Image 20.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us