How to Export Crystal Reports Without Using the Report Viewer






4.29/5 (4 votes)
This article will show how to eliminate the Crystal Reports report viewer to allow reports to be exported in one step.
Introduction
This article will show how to eliminate the Crystal Reports report viewer to allow reports to be exported in one step.
Background
I have found the report viewer to be very difficult to use for novice users. The two-step process of viewing a report and then exporting it to another program to print can be very confusing.
Using the Code
The code below was written in Visual Studio 2005. Start by creating a new website or add the code to an existing website.
Add references to the following .NET libraries.
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Shared
Create a new class module named clsSelect.vb and save it in the App_Code directory. Add the code below:
Imports Microsoft.VisualBasic
Public Enum ExportType
Excel
ExcelRecord
Pdf
MSWord
End Enum
Public Class clsSelect
Public ExportType As ExportType
Public Country As String
Public ReportExportType As Integer
Public ReportSource As String
Public ReportType As String
Public SelectionFormula As String
End Class
Create a second class module named clsCRExport.vb and save it in the App_Code directory. Add the code below:
Imports Microsoft.VisualBasic
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class clsCRExport
Inherits System.Web.UI.Page
Public Function CRExportReport(ByVal sel As clsSelect) As String
'Remember change the reg. key to -1 if needed
'HKEY_LOCAL_MACHINE\SOFTWARE\Crystal Decisions\10.2\
'Report Application Server\ InprocServer\PrintJobLimit
'Get the temporary file name
Dim sTempFileName As String = System.IO.Path.GetTempFileName
Dim ContentType As String = ""
Try
Dim Report As New ReportDocument
'Load the report
Report.Load(System.AppDomain.CurrentDomain.BaseDirectory() & sel.ReportSource)
'Get the selection formula if there is one
If Len(sel.SelectionFormula) > 1 Then
Report.RecordSelectionFormula = sel.SelectionFormula
End If
'Set the file options, temporary file name and content type
Dim FileOption As New DiskFileDestinationOptions
FileOption.DiskFileName = sTempFileName
Dim Options As New ExportOptions
Options.ExportDestinationOptions = FileOption
Options.ExportDestinationType = ExportDestinationType.DiskFile
Select Case sel.ReportExportType
Case ExportType.ExcelRecord
Options.ExportFormatType = ExportFormatType.ExcelRecord
Report.ExportToDisk(ExportFormatType.ExcelRecord, sTempFileName)
ContentType = "application/vnd.ms-excel"
Case ExportType.Excel
Options.ExportFormatType = ExportFormatType.Excel
Report.ExportToDisk(ExportFormatType.Excel, sTempFileName)
ContentType = "application/vnd.ms-excel"
Case ExportType.Pdf
Options.ExportFormatType = ExportFormatType.PortableDocFormat
Report.ExportToDisk(ExportFormatType.PortableDocFormat, sTempFileName)
ContentType = "application/pdf"
Case ExportType.MSWord
Options.ExportFormatType = ExportFormatType.WordForWindows
Report.ExportToDisk(ExportFormatType.WordForWindows, sTempFileName)
ContentType = "application/msword"
End Select
'Write the file to the web page
System.Web.HttpContext.Current.Response.ClearContent()
System.Web.HttpContext.Current.Response.ClearHeaders()
System.Web.HttpContext.Current.Response.ContentType = ContentType
System.Web.HttpContext.Current.Response.WriteFile(sTempFileName)
'Clean up the response object
System.Web.HttpContext.Current.Response.Flush()
System.Web.HttpContext.Current.Response.Close()
'IMPORTANT do the garbage collection
Report.Dispose()
FileOption = Nothing
Options = Nothing
System.IO.File.Delete(sTempFileName)
Catch ex As Exception
System.Diagnostics.Debug.Print(ex.ToString)
Return ex.ToString
End Try
Return "OK"
End Function
End Class
Next create a web form named Default.aspx with the following controls:
- Drop down list named
ddlFileType
with the following items: - Excel
- Excel Record
- Word
- Drop down list named
ddlCountry
with the following items: - England
- France
- USA
- Command button named
btnReport
. Change the text to Get Report.
Add the code below the btnReport_Click sub.
<span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt">
Protected Sub btnReport_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnReport.Click
Dim sel As New clsSelect
'Set the report export type
Select Case ddlFileType.Text
Case "Pdf"
sel.ReportExportType = ExportType.Pdf
Case "Excel"
sel.ReportExportType = ExportType.Excel
Case "Excel Record"
sel.ReportExportType = ExportType.ExcelRecord
Case "Word"
sel.ReportExportType = ExportType.MSWord
End Select
'Set the selection formula
sel.SelectionFormula = "{customer.Country} = '" & ddlCountry.Text & "'"
'Set the report source
sel.ReportSource = "CrystalReport.rpt"
'Set the session object
Session("Select") = sel
'Load the report viewer
Response.Redirect("ReportViewer.aspx")
End Sub
</span>
Create a second web form named ReportViewer.aspx and add the code below to the Page_Load sub.
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
'No session object
If Session("Select") Is Nothing = True Then
Response.Redirect("Default.aspx")
Exit Sub
End If
Dim sel As New clsSelect
sel = Session("Select")
Dim cr As New clsCRExport
Dim ResponseString As String = ""
Try
'Load the report
ResponseString = cr.CRExportReport(sel)
'Get the error
If ResponseString <> "OK" Then
System.Diagnostics.Debug.Print(ResponseString)
End If
Catch ex As Exception
Response.Redirect("Default.aspx")
End Try
End Sub
If you did not download the source code, create a new Crystal Report and change the Report source and Selection formula located Default.aspx.vb.
Note: If you have a problem with the Crystal Decisions engine, remove the <assemblies>
area of the web.config file and add references to the .NET libraries below.
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Shared
History
- Version 1.0