HTML SOURCE OF PAGE
Write code mentioned below in Page_Load Event of page to display crystal report on page load and another code in Click Event of Button to Export report to pdf or other formats.
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.
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>
Write code mentioned below in Page_Load Event of page to display crystal report on page load and another code in Click Event of Button to Export report to pdf or other formats.
C# CODE
01using CrystalDecisions.CrystalReports.Engine;02using CrystalDecisions.Shared;03protected void Page_Load(object sender, EventArgs e)04 {05 ReportDocument pdfReport = new ReportDocument();06 pdfReport.Load(Server.MapPath("ExportToPdf.rpt"));07 pdfReport.SetDatabaseLogon("amitjain","password", @"AMITJAIN\SQL", "Northwind");08 CrystalReportViewer1.ReportSource = pdfReport;09 }10 protected void btnExport_Click(object sender, EventArgs e)11 {12 ReportDocument pdfReport = new ReportDocument();13 pdfReport.Load(Server.MapPath("ExportToPdf.rpt"));14 pdfReport.SetDatabaseLogon("amitjain", "password", @"AMITJAIN\SQL", "Northwind");15 Response.Buffer = false;16 Response.ClearContent();17 Response.ClearHeaders();18 pdfReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Employees");19 Response.End();20 }VB.NET CODE
01Protected Sub Page_Load(sender As Object, e As EventArgs)02 Dim pdfReport As New ReportDocument()03 pdfReport.Load(Server.MapPath("ExportToPdf.rpt"))04 pdfReport.SetDatabaseLogon("amitjain", "password", "AMITJAIN\SQL", "Northwind")05 CrystalReportViewer1.ReportSource = pdfReport06End Sub07Protected Sub btnExport_Click(sender As Object, e As EventArgs)08 Dim pdfReport As New ReportDocument()09 pdfReport.Load(Server.MapPath("ExportToPdf.rpt"))10 pdfReport.SetDatabaseLogon("amitjain", "password", "AMITJAIN\SQL", "Northwind")11 Response.Buffer = False12 Response.ClearContent()13 Response.ClearHeaders()14 pdfReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "Employees")15 Response.[End]()16End SubBuild 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
1pdfReport.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, "Employees");Where Employees is the name of file you want to be created.
For MS Excel
1pdfReport.ExportToHttpResponse(ExportFormatType.Excel, Response, true, "Employees");
0 comments:
Post a Comment