如何在VB.NET中以PDF格式编写水晶报表

时间:2013-01-15 10:42:03

标签: .net vb.net crystal-reports

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Web
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.CrystalReports

Imports System.IO
Imports System.Net
Imports System.Net.Mail

Public Class Form1

Dim cryRpt As New ReportDocument

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    cryRpt.Load("C:\Documents and Settings\Prs1\My Documents\Visual Studio 2008\Projects\myCR\myCR\cr.rpt")
    CrystalReportViewer1.ReportSource = cryRpt

    CrystalReportViewer1.Refresh()

    Try
        Dim CrExportOptions As ExportOptions
        Dim CrDiskFileDestinationOptions As New  _
        DiskFileDestinationOptions()
        Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
        CrDiskFileDestinationOptions.DiskFileName = _
                                    "C:\crystalExport.pdf"
        CrExportOptions = cryRpt.ExportOptions
        With CrExportOptions
            .ExportDestinationType = ExportDestinationType.DiskFile
            .ExportFormatType = ExportFormatType.PortableDocFormat
            .DestinationOptions = CrDiskFileDestinationOptions
            .FormatOptions = CrFormatTypeOptions
        End With
        cryRpt.Export()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub

结束班

我尝试了上面的代码。我的项目文件夹中有一个Windows窗体和一个Crystal报表文件(cr.rpt)。我正在使用Oracle 10G作为数据库。 但是我收到一条错误,说“登录失败”。 ([错误说明]图片:http://pho.to/Zv6t

请帮助。

3 个答案:

答案 0 :(得分:1)

最后找到here

的解决方案

唯一的问题是,我需要在运行时设置数据库凭据才能使Crystal Report正常工作。

cryRpt.SetDatabaseLogon("user", "password", "server", "database")

答案 1 :(得分:0)

这个常见问题只发生在从VB到.Net世界的旧版CrystalReports调用者。

没什么可担心的。请使用这样.........

connectionInfo.DatabaseName = "MahendrenDataBase"

connectionInfo.ServerName ="SystemDSN";//Create a system dsn.

connectionInfo.UserID = "UID";

connectionInfo.Password="Pwd"

答案 2 :(得分:0)

导入CrystalDecisions.CrystalReports.Engine

导入CrystalDecisions.Shared

Dim rpt As New RptCashbill

        ''''''fill Report data''''''
  CType(rpt,ReportDocument).ExportToDisk(ExportFormatType.PortableDocFormat,"C:/Report.pdf")