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.
- Create an Employee Table as in the following.
- create table EmployeeInformation
- (
- EmpId int identity(1, 1) Primary Key,
- EmpName varchar(20),
- EmpContact nchar(15),
- EmpDeparatment varchar(30),
- EmpCity varchar(30)
- )
In this table EmpId is an auto-increment field for Employee identity.
- Create Procedure
Now create a select Stored Procedure for Employee information as in the following:
- Create procedure sp_EmployeeInformation
- As
- Begin
- Set nocount on;
- Select EmpName,EmpContact,EmpDeparatment,EmpCity
- From EmployeeInformation
- End
- Insert some employee records into the table as shown below:
- insert into EmployeeInformation values('Jonny','9988774445','IT','Delhi')
- insert into EmployeeInformation values('Karan','9987534445','EC','Pune')
- insert into EmployeeInformation values('Mitali','9988774741','HR','Kolkata')
- insert into EmployeeInformation values('Rohan','8888774445','IT','Jaipur')
- insert into EmployeeInformation values('Sapnil','9988714785','HR','Gurgaon')
- insert into EmployeeInformation values('Sonika','9989994445','ELC','Bangalore')
- insert into EmployeeInformation values('Punit','9888774445','ACC','Delhi')
- insert into EmployeeInformation values('Ketan','9988774445','IT','Chennai')
- insert into EmployeeInformation values('Jennit','9986541235','EC','Delhi')
- 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
Figure 2: Add Crystal Report
Figure 3: Select Report Gallery
Figure 4: ADO Connection
Figure 5: Select SQL Server Native Client
Figure 6: Provide Database Connection Information
Figure 7: Connection Information
Figure 8: Select Created Procedure
Figure 9: Choose Field
Figure 10: Choose Report Display Format
Finally your Design Page is ready as in the following figure,
Figure 11: Report Design Format
STEP 4
Go to Solution Explorer and add a new item.
Figure 11: Add UI
STEP 5
Add a Report Viewer.
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:
- <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="ImployeeInformation.aspx.cs" Inherits="Test_WebApplication.ImployeeInformation" %>
- <%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>
- <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
- </asp:Content>
- <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
- <div>
- <h2>Report Data Export In PDF, Word and Excel Document </h2>
- <table style="width: 700px">
- <tr>
- <td>Save Report Data In Different Format</td>
- <td><asp:Button ID="btnPDF" runat="server" Text="PDF Format" onclick="btnPDF_Click" /></td>
- <td><asp:Button ID="btnExcel" runat="server" Text="EXCEL Format" onclick="btnExcel_Click"/></td>
- <td><asp:Button ID="btnWord" runat="server" Text="WORD Format"
- onclick="btnWord_Click" Width="129px" /></td>
- </tr>
- </table>
- </div>
- <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
- AutoDataBind="true" />
- </asp:Content>
STEP 7UI CODE SIDEIn 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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using CrystalDecisions.CrystalReports.Engine;
- using CrystalDecisions.Shared;
-
- namespace Test_WebApplication {
- public partial class ImployeeInformation: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ReportDocument Report = new ReportDocument();
- Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));
- Report.SetDatabaseLogon("sa", " password ", "Rakesh-PC", "SqlServerTech");
- CrystalReportViewer1.ReportSource = Report;
- }
-
- protected void btnPDF_Click(object sender, EventArgs e)
- {
- ReportDocument Report = new ReportDocument();
- Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));
- Report.SetDatabaseLogon("sa", "password ", "Rakesh-PC", "SqlServerTech");
- Response.Buffer = false;
- Response.ClearContent();
- Response.ClearHeaders();
- Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "EmployeeInformation");
- Response.End();
- }
-
- protected void btnExcel_Click(object sender, EventArgs e)
- {
- ReportDocument Report = new ReportDocument();
- Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));
- Report.SetDatabaseLogon("sa", "password", "Rakesh-PC", "SqlServerTech");
- Response.Buffer = false;
- Response.ClearContent();
- Response.ClearHeaders();
- Report.ExportToHttpResponse(ExportFormatType.Excel, Response, true, "EmployeeInformation");
- Response.End();
- }
-
- protected void btnWord_Click(object sender, EventArgs e)
- {
- ReportDocument Report = new ReportDocument();
- Report.Load(Server.MapPath("~/Q:/ Test_WebApplication/EmployeeInfo.rpt"));
- Report.SetDatabaseLogon("sa", " password ", "Rakesh-PC", "SqlServerTech");
- Response.Buffer = false;
- Response.ClearContent();
- Response.ClearHeaders();
- Report.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, "EmployeeInformation");
- Response.End();
- }
- }
- }
STEP 8BROWSER SIDE
Now you can run the report in the browser and see the following:
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.