Click here to Skip to main content
15,793,260 members
Articles / Web Development / ASP.NET

How to Export Crystal Reports Without Using the Report Viewer

Rate me:
Please Sign up or sign in to vote.
4.29/5 (4 votes)
14 Jan 2011CPOL1 min read 77K   2.4K   15   8
This article will show how to eliminate the Crystal Reports report viewer to allow reports to be exported in one step.


This article will show how to eliminate the Crystal Reports report viewer to allow reports to be exported in one step.


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.


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
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 = ""


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/"

Case ExportType.Excel
	Options.ExportFormatType = ExportFormatType.Excel
	Report.ExportToDisk(ExportFormatType.Excel, sTempFileName)
	ContentType = "application/"

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.ContentType = ContentType

'Clean up the response object

'IMPORTANT do the garbage collection
FileOption = Nothing
Options = Nothing


Catch ex As Exception

	Return ex.ToString

End Try

Return "OK"

End Function

End Class

Next create a web form named Default.aspx with the following controls:

  1. Drop down list named ddlFileType with the following items:
    1. Pdf
    2. Excel
    3. Excel Record
    4. Word
  2. Drop down list named ddlCountry with the following items:
    1. England
    2. France
    3. USA
  3. 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

End Sub

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


Exit Sub

End If

Dim sel As New clsSelect

sel = Session("Select")

Dim cr As New clsCRExport

Dim ResponseString As String = ""


'Load the report

ResponseString = cr.CRExportReport(sel)

'Get the error

If ResponseString <> "OK" Then


End If

Catch ex As Exception


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.



  • Version 1.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer David Weaver Consulting
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionProblems with new browser updates Pin
David Weaver1-Aug-13 4:12
David Weaver1-Aug-13 4:12 
QuestionBlank display while displaying pdf document which is not included in Project. Pin
Nawang Lama31-Jul-13 21:18
Nawang Lama31-Jul-13 21:18 
Questionerror Pin
mahi159021-Mar-13 2:49
mahi159021-Mar-13 2:49 
QuestionThanks!! Pin
KaushikChandra4-Jan-12 0:28
KaushikChandra4-Jan-12 0:28 
GeneralThanks! Pin
hoodaticus21-Jan-11 4:14
hoodaticus21-Jan-11 4:14 
GeneralRe: Thanks! Pin
David Weaver21-Jan-11 4:18
David Weaver21-Jan-11 4:18 
Generalthanks for sharing - have 5 Pin
Pranay Rana16-Jan-11 21:35
professionalPranay Rana16-Jan-11 21:35 
GeneralMy vote of 5 Pin
alaminfad14-Jan-11 20:28
alaminfad14-Jan-11 20:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.