Tip: How to Export a Crystal Report to a PDF file Programmatically in ASP.NET?


You can export a Crystal Report to various formats using CrystalReportViewer's Export option. However, there are occasions when you want your users to see the report direct in PDF format in the browser as soon they click the Report button in your application.

 

The ReportDocument class provides Export methods to export a report to various formats including PDF, Excel, Word, and HTML.

 

The following steps will guide you to achieve the same:

 

  1. Add crystal report (.cr) file to your ASP.NET application.
  2. Add a report instance on the page level.

    Dim report As MyReport = New MyReport

 

  1. Populate reports data on Page_Init  

     
    ' Get data in a DataSet or DataTable

        Dim ds As DataSet = GetData()

        ' Fill report with the data

     report.SetDataSource(ds)

 

  1. Export Report
    report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "ExportedReport")

 

If you wish to format report to other formats, just change the ExportFormatType enumeration value to your desired format.

 

If you wish to download the report, then you simply change the third parameter of ExportToHttpResponse method in Step 4 to True.

 

The following code shows how to export a report to other formats including Word and Excel.

 

' Export report to the selected format

If ExportFormatList.SelectedItem.Text = "Adobe (PDF)" Then

                report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "ExportedReport")

            ElseIf ExportFormatList.SelectedItem.Text = "MS Word" Then

                report.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, True, "ExportedReport")

            ElseIf ExportFormatList.SelectedItem.Text = "MS Excel 97 - 2000" Then

                report.ExportToHttpResponse(ExportFormatType.Excel, Response, True, "ExportedReport")

            ElseIf ExportFormatList.SelectedItem.Text = "MS Excel 97 - 2000 (Data Only)" Then

                report.ExportToHttpResponse(ExportFormatType.ExcelRecord, Response, True, "ExportedReport")

            End If

Up Next
    Ebook Download
    View all
    Learn
    View all