This example explains how to Export or Save Crystal Reports To Pdf,Ms Word,Excel Programmatically In Asp.Net
For this Example i have used Northwind Database and Employees table.
After you have finished Creating Crystal Reports,
place one button on the aspx page to export report to pdf or word format.
We'll write code in Page_Load Event to load report when page loads and in Click Event of button to create pdf,word or excel report.
HTML SOURCE OF PAGE
C#
VB.NET
Build and run the code.
To Export crystal Reports to Word Excel or other formats we need to change below mentioned line of code to desired format.
Where Employees is the name of file you want to be created.
For this Example i have used Northwind Database and Employees table.
After you have finished Creating Crystal Reports,
place one button on the aspx page to export report to pdf or word format.
We'll write code in Page_Load Event to load report when page loads and in Click Event of button to create pdf,word or excel report.
HTML SOURCE OF PAGE
1: <form id="form1" runat="server">
2: <div>
3: <asp:Button ID="btnExport" runat="server"
4: Text="Export To PDF"
5: onclick="btnExport_Click"/>
6: </div>
7: <div>
8: <CR:CrystalReportSource ID="CrystalReportSource1"
9: runat="server">
10: <Report FileName="ExportToPdf.rpt">
11: </Report>
12: </CR:CrystalReportSource>
13:
14: <CR:CrystalReportViewer ID="CrystalReportViewer1"
15: runat="server"
16: AutoDataBind="True"
17: EnableDatabaseLogonPrompt="False"
18: EnableParameterPrompt="False"
19: ReportSourceID="CrystalReportSource1"/>
20: </div>
21: </form>
C#
using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; protected void Page_Load(object sender, EventArgs e) { ReportDocument pdfReport = new ReportDocument(); pdfReport.Load(Server.MapPath("ExportToPdf.rpt")); pdfReport.SetDatabaseLogon("amitjain","password", @"AMITJAIN\SQL", "Northwind"); CrystalReportViewer1.ReportSource = pdfReport; } protected void btnExport_Click(object sender, EventArgs e) { ReportDocument pdfReport = new ReportDocument(); pdfReport.Load(Server.MapPath("ExportToPdf.rpt")); pdfReport.SetDatabaseLogon("amitjain", "password", @"AMITJAIN\SQL", "Northwind"); Response.Buffer = false; Response.ClearContent(); Response.ClearHeaders(); pdfReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Employees"); Response.End(); }
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs) Dim pdfReport As New ReportDocument() pdfReport.Load(Server.MapPath("ExportToPdf.rpt")) pdfReport.SetDatabaseLogon("amitjain", "password", "AMITJAIN\SQL", "Northwind") CrystalReportViewer1.ReportSource = pdfReport End Sub Protected Sub btnExport_Click(sender As Object, e As EventArgs) Dim pdfReport As New ReportDocument() pdfReport.Load(Server.MapPath("ExportToPdf.rpt")) pdfReport.SetDatabaseLogon("amitjain", "password", "AMITJAIN\SQL", "Northwind") Response.Buffer = False Response.ClearContent() Response.ClearHeaders() pdfReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "Employees") Response.[End]() End Sub
Build and run the code.
To Export crystal Reports to Word Excel or other formats we need to change below mentioned line of code to desired format.
For MS WORD
pdfReport.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, "Employees");
Where Employees is the name of file you want to be created.
For MS Excel
pdfReport.ExportToHttpResponse(ExportFormatType.Excel, Response, true, "Employees");
If you like this post than join us or share
2 comments:
nice work sir
thax..
Post a Comment