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
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#
01
using
CrystalDecisions.CrystalReports.Engine;
02
using
CrystalDecisions.Shared;
03
protected
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
01
Protected
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 = pdfReport
06
End
Sub
07
Protected
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 =
False
12
Response.ClearContent()
13
Response.ClearHeaders()
14
pdfReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response,
True
,
"Employees"
)
15
Response.[
End
]()
16
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
1
pdfReport.ExportToHttpResponse(ExportFormatType.WordForWindows, Response,
true
,
"Employees"
);
Where Employees is the name of file you want to be created.
For MS Excel
1
pdfReport.ExportToHttpResponse(ExportFormatType.Excel, Response,
true
,
"Employees"
);
No comments:
Post a Comment