Export Crystal Reports To PDF Word Excel In Asp.Net

Export crystal reports to pdf word excel in asp.net
Export or save crystal reports to pdf,ms word,excel programmatically in asp.net

In this post i'm explaining how to export or save crystal reports to pdf,word or excel programmatically in button click event.

For this Example i have used Northwind Database and Employees table.

Read Crystal reports in ASP.NET to know how to create crystal reports in asp.net.

After you have finished creating crystal report as described in link above, place one button on the aspx page to export crystal report to pdf or word format.



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>

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
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 CODE
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