Thursday, February 11, 2010

Export Crystal report to pdf file without showing report

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Dim daPref As New SqlDataAdapter
Dim dsPref As New DataSet
Dim clsCom As New clsCommon
Dim PDFFilename As String = String.Empty
Dim SqlConn As New SqlConnection
Dim crtableLogoninfos As New CrystalDecisions.Shared.TableLogOnInfos
Dim crtableLogoninfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim crConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo
Dim CrTables As Tables
Dim CrTable As Table
Dim crpReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
'Try
crpReport.Load(Server.MapPath("~") & "\PDFView.rpt")
With crConnectionInfo
.ServerName = System.Configuration.ConfigurationManager.AppSettings("ServerAddress")
.DatabaseName = System.Configuration.ConfigurationManager.AppSettings("ServerDB")
.UserID = System.Configuration.ConfigurationManager.AppSettings("ServerUser")
.Password = System.Configuration.ConfigurationManager.AppSettings("ServerPassword")
End With
CrTables = crpReport.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
CrTable.Location = System.Configuration.ConfigurationManager.AppSettings("ServerDB") & ".dbo." & CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)
Next
crpReport.SetParameterValue(0, ProjectId)
'CRViewer.ReportSource = crpReport
crpReport.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
crpReport.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
PDFFilename = Server.MapPath("~") & "\" & ProjectId & " - Project Request - " & txtProjectTitle.Text.Trim() & ".pdf"
crpReport.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, PDFFilename)

No comments:

Post a Comment