There are two simple ways to send a Crystal Report to the client's browser in ASP.NET.  One uses the ExportOptions properties of a ReportDocument to set where a file should be saved, and then the Export() method to save the report file.  The second method uses the ExportToStream() method of a ReportDocument to serialize the file into a byte array, and send the byte array directly to the client's browser.

This first example saves the file to the server's hard drive, and uses a response.redirect to send the user to the file.  The redirect will automatically trigger the browser's download handler (that pop-up box with the Open/Save options) for the file.  The example below exports a PDF file to a subdirectory below my web application root.  The ASPNET user has Modify permission on this subdirectory.

'Make sure you have these imports in your file
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO

'...

DIM rptSummary as ReportDocument
Dim strExportFile As String
Dim objExOpt As ExportOptions
Dim objDiskOpt As New DiskFileDestinationOptions

strExportFile = "incident_count_detailed.pdf"

' ... process your report here ...

objDiskOpt.DiskFileName = Server.MapPath(".") & "/export/" & strExportFile
objExOpt = rptSummary.ExportOptions
objExOpt.ExportDestinationType = ExportDestinationType.DiskFile
objExOpt.ExportFormatType = ExportFormatType.PortableDocFormat
objExOpt.DestinationOptions = objDiskOpt
rptSummary.Export()
rptSummary.Close()

Response.Redirect("export/" & strExportFile)

Since the file is saved to your web server, you should protect this file by mapping it to the aspnet_isapi.dll if you are using Forms Authentication.  You will also need to devise a way to clean up the exported files.

Another option is to export the file directly to a memory stream that is directed to the browser.  This will again automatically trigger the browser's download handler.  This second method has the advantage of not cluttering up your server with exported files.

'Make sure you have these imports in your file
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO

'...

DIM rptSummary as ReportDocument
Dim strExportFile As String

strExportFile = "incident_count_detailed.pdf"

' ... process your report here ...

Dim s As System.IO.MemoryStream = rptSummary.ExportToStream(ExportFormatType.PortableDocFormat)
With HttpContext.Current.Response
.ClearContent()
.ClearHeaders()
.ContentType = "application/pdf"
.AddHeader("Content-Disposition", "inline; filename=" & strExportFile)
.BinaryWrite(s.ToArray)
.End()
End With

<edit 2004-07-19>

One user in the Crystal Reports Forum on ASP.NET reported the following error:
(source: http://asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=629791#640177)

When I attempt to export a report to HTML3.2 or HTML4.0 using the ExportToStream() method, I get a system.io.filenotfoundexception thrown. The funny thing is that the report exports to pdf, doc, and excel format just fine.

We couldn't figure out what was wrong, so the user queried Business Objects.  This was their reply:

I would like to share some more information with you about ExportToStream to point out that it is an undocumented and unsupported method. If you were to uncover a bug in the exportToStream, we would not be able to have the bug fixed. Also, compared to a regular export, there is little or no performance gain when using this method because a temporary file is created on the disk. It might be in your best interests to use the Export() method, stream the file and then delete the disk file.

Despite what BO claims, ReportDocument.ExportToStream() is documented in the VS .NET help files.  Moral of the story: use ExportToStream() at your own risk.

<edit 2004-08-27>

When the fact that ExportToStream() is documented was presented to Business Objects, this was their reply:

The ExportToStream method has some issues in Crystal Reports for .NET and hence we do not recommend and support the same.

The correct way to export is to use the export method from the reportdocument object (report.Export()) where the file is exported to disk first and then streamed to the browser. As already mentioned, it might be in your best interests to use the Export() method, stream the file and then delete the disk file.

The entire thread can be found here:
http://asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=629791

Crystal Reports .NET Programming
If you're new to Crystal Reports, or just to CR .NET, this is the book you need. Part how-to, part programming reference, lots of simple examples in both C# and VB.NET.

 

 

 

 

Sponsor