Here is code that I use all the time, to export a crystal report to a pdf
from a webpage.
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Web.Report
Private m_Rpt As ReportDocument = New ReportDocument
Private m_ReportPath As String
Dim connection As IConnectionInfo
Dim userID As String = "sa"
Dim password As String = "mypassword"
m_Rpt.Load(m_ReportPath)
m_Rpt.SetDatabaseLogon(userID, password)
Dim subreport As ReportDocument
For Each subreport In m_Rpt.Subreports
For Each connection In subreport.DataSourceConnections
connection.SetLogon(userID, password)
Next
Next
Now to decifer some.
The imports, obvious.
Two variables one for the path one for the report.
The path must be a file based path. It can be a UNC path.
Next setup the connection params.
Load the report, then set the login params for the database.
The loop is for all subreports within the main report.
You can include these even if the report does not contain subreports
Now we setup the export part. This below is its own procedure in my
world.
Dim crExportOptions As ExportOptions
crExportOptions = m_Rpt.ExportOptions
With crExportOptions
Select Case Request.QueryString("format") 'rptOptions.SelectedItem.ToString
Case "PDF"
.FormatOptions = New PdfRtfWordFormatOptions()
.ExportFormatType = ExportFormatType.PortableDocFormat
Case "Word"
.FormatOptions = New PdfRtfWordFormatOptions()
.ExportFormatType = ExportFormatType.WordForWindows
Case "Excel"
.FormatOptions = New ExcelFormatOptions
.ExportFormatType = ExportFormatType.Excel
Case "ExcelDO"
.FormatOptions = New ExcelFormatOptions
.ExportFormatType = ExportFormatType.ExcelRecord
End Select
.ExportDestinationType = ExportDestinationType.NoDestination
End With
Dim req As ExportRequestContext = New ExportRequestContext()
req.ExportInfo = crExportOptions
Dim st As System.IO.Stream
st = m_Rpt.FormatEngine.ExportToStream(req)
Response.ClearContent()
Response.ClearHeaders()
Select Case Request.QueryString("format")
Case "PDF"
Response.ContentType = "application/pdf"
Case "Word"
Response.ContentType = "application/msword"
Case "Excel"
Response.ContentType = "application/x-msexcel"
Case "ExcelDO"
Response.ContentType = "application/x-msexcel"
End Select
Dim b(st.Length) As Byte
st.Read(b, 0, st.Length)
Response.BinaryWrite(b)
Response.End()
Once again to decifer:
I am using a web page for the interface so I am selecting the
exportoptions based on what the user wants.
The bottom half of this sub is for exporting to a web page and
getting the pdf to open in the page.
I think if you just save the st stream to disk will work fine.
I left out the code where I am passing params to the report.
HTH
Ron