Click here to Skip to main content
15,884,298 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 77.7K   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.

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.

VB.NET
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:

VB.NET
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:

VB.NET
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:

  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.

VB.NET
<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.

VB.NET
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.

VB.NET
CrystalDecisions.CrystalReports.Engine 
CrystalDecisions.Shared 

History

  • Version 1.0

License

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 3:12
David Weaver1-Aug-13 3:12 
QuestionBlank display while displaying pdf document which is not included in Project. Pin
Nawang Lama31-Jul-13 20:18
Nawang Lama31-Jul-13 20:18 
Questionerror Pin
mahi159021-Mar-13 1:49
mahi159021-Mar-13 1:49 
QuestionThanks!! Pin
KaushikChandra3-Jan-12 23:28
KaushikChandra3-Jan-12 23:28 
Thank you very much for the article.It helped me a lot.

But I am having trouble with the reports when the data is large(30,000 records). I tested the report with different sizes of data starting from 5,000 to 25,000. The problem starts when the record count increases beyond that.

I am getting an error like

Logon failed. Details: mscorlib : Exception of type 'System.OutOfMemoryException' was thrown. Error in File C:\Users\username\AppData\Local\Temp\TestReport {A5C0F499-43EF-47C2-B5BE-3B0ED6C7AAF3}.rpt: Unable to connect: incorrect log on parameters.

I had changed the registry editor value

HKEY_LOCAL_MACHINE\SOFTWARE\Crystal Decisions\10.2\Report Application Server\
InprocServer\PrintJobLimit to -1

My requirement says the record count my go upto 50k records.

can you please help me in this.

Thank you.
GeneralThanks! Pin
hoodaticus21-Jan-11 3:14
hoodaticus21-Jan-11 3:14 
GeneralRe: Thanks! Pin
David Weaver21-Jan-11 3:18
David Weaver21-Jan-11 3:18 
Generalthanks for sharing - have 5 Pin
Pranay Rana16-Jan-11 20:35
professionalPranay Rana16-Jan-11 20:35 
GeneralMy vote of 5 Pin
alaminfad14-Jan-11 19:28
alaminfad14-Jan-11 19: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.