vb.net将参数传递给现有的水晶报表并导出

时间:2017-01-05 23:52:21

标签: c# vb.net crystal-reports

我有一个接受一个参数的现有水晶报告。我想使用VB.Net或C#打开报告,传递参数然后将其导出为PDF。

此外,这需要是一个简单启动的控制台应用程序。这不需要在网站上。无需UI。

非常感谢任何帮助。

最近一次尝试的代码:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.shared
Imports CrystalDecisions.Windows.Forms
Imports System.Windows.Forms
Imports CrystalDecisions.Web
Imports CrystalDecisions.CrystalReports
Imports System.IO
Imports System.Net

Module Module1
Sub main()
    Dim myReport As New ReportDocument
    Dim crystalreportviewer As New CrystalReportViewer
    Dim param1Fileds As New CrystalDecisions.Shared.ParameterFields
    Dim param1Field As New CrystalDecisions.Shared.ParameterField
    Dim param1Range As New CrystalDecisions.Shared.ParameterDiscreteValue
    Dim CRYRpt1 As New CrystalReportViewer

    Dim myParams = New ParameterFields()
    Dim myParam = New ParameterField()
    Dim myDiscreteValue = New ParameterDiscreteValue()

    myParam.ParameterFieldName = "@SerialNumber"
    myDiscreteValue.Value = "DA1714625"
    myParam.CurrentValues.Add(myDiscreteValue)

    myParams.Add(myParam)

    Dim cryRpt As ReportDocument

    cryRpt = New ReportDocument()

    cryRpt.Load("c:\users\cjniva\6850.rpt")


    Dim CrExportOptions As ExportOptions
    Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
    Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
    CrDiskFileDestinationOptions.DiskFileName = "C:\\Data\\Data1\\DemoDates.pdf"

    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions
    CrExportOptions.FormatOptions = CrFormatTypeOptions
    cryRpt.Export()

End Sub

最新版本:

Sub main()

    'Private Sub RunReport()
    Dim report As New ReportDocument
    report.Load("c:\users\cjniva\desktop\6850.rpt")
    report.SetDatabaseLogon("dba", "pw")
    report.SetParameterValue("SerialNumber", "DA3EEXQTO")
    'report.ReadRecords()
    Dim exportFilename As String
    exportFilename = "test.pdf"
    ExportCrystalReport(report, ExportFormatType.PortableDocFormat, exportFilename)
End Sub

Private Sub ExportCrystalReport(ByRef report As ReportDocument, ByVal exportType As CrystalDecisions.Shared.ExportFormatType, ByVal exportFilename As String)
    Dim expFilename As String = Path.Combine("c:\users\cjniva\desktop\", exportFilename)
    Dim diskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions()
    diskOpts.DiskFileName = expFilename
    Dim exportOpts As CrystalDecisions.Shared.ExportOptions = New CrystalDecisions.Shared.ExportOptions()
    exportOpts.ExportFormatType = exportType
    exportOpts.ExportDestinationOptions = diskOpts
    exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
    report.Export(exportOpts)

End Sub

2 个答案:

答案 0 :(得分:0)

这是一个简单的布局,包括导出部分。

$Max_id + 1

答案 1 :(得分:0)

Crystal字体问题。将所有字段更改为Arial之后,一切正常。