Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create reports as .pdf files 1

Status
Not open for further replies.

atropozzz

Programmer
Mar 19, 2002
16
AT
ciao a tutti!

for my company i have to solve the problem of creating reports out of a database. the reports should be written into .pdf-files.

anyone got an idea (a vb.net class + description) how to handle thizzz?

funk ya very much.

regards, atropozzz
 
Are you using Visual Studio.net?
If so, just use the built-in crystal reports for .net.
Crystal has a bunch of samples that show how to export the reports to different file types.

I first added a dataset to my project that was based on the resultset of a SQL Server stored procedure. Then I added a new crystal report to my VS.net project and based it on the dataset. Here's the code I use to fill the dataset with the results of the stored procedure, bind the report to the dataset, display the report in a crystal report viewer, and export the report to both xls and pdf files:

Private Sub RunVarianceReport()
Dim cn As New SqlClient.SqlConnection()
Dim cmd As New SqlClient.SqlCommand()
Dim da As New SqlClient.SqlDataAdapter()
Dim oRpt As New VarianceReport() 'Name of my report
Dim ds As New VarianceDataset() 'Name of my dataset
Dim strExportFilePath As String
Dim DiskOpts As New CrystalDecisions.Shared.DiskFileDestinationOptions()

cmd.CommandTimeout = 600
cn.ConnectionString = Global.CONNECTION_STRING
cmd.Connection = cn
cmd.CommandText = "stored proc name"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@Param1", SqlDbType.NVarChar, 10)
cmd.Parameters("@Param2").Value = Global.SAP_INSTANCE
cmd.Parameters.Add("@Return", SqlDbType.NVarChar, 4000)
cmd.Parameters("@Return").Direction = ParameterDirection.Output

da.SelectCommand = cmd
da.Fill(ds, "VarianceReportData")

' Set the SetDataSource property of the Report to the Dataset
oRpt.SetDataSource(ds)

'Display the report in the viewer
CrystalReportViewer1.Visible = True
CrystalReportViewer1.ReportSource = oRpt

'Specify export type as DiskFile
oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile

'Export to PDF
strExportFilePath = "c:\whatever\ & Session.SessionID & ".pdf"
oRpt.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
DiskOpts.DiskFileName = strExportFilePath
oRpt.ExportOptions.DestinationOptions = DiskOpts
oRpt.Export()

'Export to XLS
strExportFilePath = "c:\whatever\" & Session.SessionID & ".xls"
oRpt.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel
DiskOpts.DiskFileName = strExportFilePath
oRpt.ExportOptions.DestinationOptions = DiskOpts
oRpt.Export()

'Set up links to the exported files
linkPDF.NavigateUrl = "c:\whatever\" & Session.SessionID & ".pdf"
linkPDF.Visible = True
linkXLS.NavigateUrl = "c:\whatever\" & Session.SessionID & ".xls"
linkXLS.Visible = True
End Sub
 
thanks a lot millerk! it works! great help!
it was not too hard to check this out ...

buonasera - atro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top