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
Dim sTempFileName As String = System.IO.Path.GetTempFileName
Dim ContentType As String = ""
Try
Dim Report As New ReportDocument
Report.Load(System.AppDomain.CurrentDomain.BaseDirectory() & sel.ReportSource)
If Len(sel.SelectionFormula) > 1 Then
Report.RecordSelectionFormula = sel.SelectionFormula
End If
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
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)
System.Web.HttpContext.Current.Response.Flush()
System.Web.HttpContext.Current.Response.Close()
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:
- Pdf
- 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
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
sel.SelectionFormula = "{customer.Country} = '" & ddlCountry.Text & "'"
sel.ReportSource = "CrystalReport.rpt"
Session("Select") = sel
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
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
ResponseString = cr.CRExportReport(sel)
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