将水晶报告导出为pdf

时间:2012-12-13 04:20:25

标签: vb.net

我想了解如何通过代码导出4个参数的水晶报告。我尝试了以下代码,并返回“无效索引错误”。

Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue
        Dim crParamenter_array As New ParamArrayAttribute
        Dim cryRpt As New ReportDocument
        Dim DefaultPrinterName As String
        Dim oPS As New System.Drawing.Printing.PrinterSettings
        Dim sFilePath As String
        Dim sPath As String
        Dim sFileLoc As String
        Dim sFileName As String
        Try
            sPath = Path.GetTempPath()
            DefaultPrinterName = oPS.PrinterName
            sFilePath = GetCrystalReport(_REPRT_CODE)
            sFileLoc = sPath + "Reports\" + _REPRT_CODE + ".rpt"
            If sFilePath = String.Empty OrElse sFilePath = Nothing Then
                TNotification.StatusBarError("File Not Found.")
                Return False
            Else
                cryRpt.Load(sFileLoc)
            End If
            If Not SetDbLogonInfo(cryRpt) Then
                TNotification.StatusBarError("Database Login Failed.")
                Return False
            Else
                crParameterDiscreteValue.Value = _empid
                crParameterDiscreteValue.Value = _month
                crParameterDiscreteValue.Value = _parea
                crParameterDiscreteValue.Value = _year

                crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
                crParameterFieldDefinition = crParameterFieldDefinitions.Item("empid")
                crParameterFieldDefinition = crParameterFieldDefinitions.Item("month")
                crParameterFieldDefinition = crParameterFieldDefinitions.Item("parea")
                crParameterFieldDefinition = crParameterFieldDefinitions.Item("year")

                crParameterValues = crParameterFieldDefinition.CurrentValues
                '   crParameterValues.Clear()
                crParameterValues.Add(crParameterDiscreteValue)
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
                cryRpt.PrintOptions.PrinterName = DefaultPrinterName
                Dim rep As CrystalDecisions.CrystalReports.Engine.FieldObject = Nothing
                'sending report to the printer
                'cryRpt.PrintToPrinter(_noOfCpys, False, 1, 100)
                Dim CrExportOptions As ExportOptions
                Dim CrDiskFileDestinationOptions As New  _
                DiskFileDestinationOptions()
                Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
                ' CrDiskFileDestinationOptions.DiskFileName = "c:\" + _rptCode + "_" + _docEntry + ".pdf"
                'sFileName = "\\B1devsrv\b1_shr\Attachments\" + _rptCode + "_" + _docEntry.ToString + ".pdf"
                sFileName = TSQL.GetSingleRecord("Select AttachPath  from OADP").ToString.Trim + _empid + "_payslip.pdf"
                CrDiskFileDestinationOptions.DiskFileName = sFileName
                CrExportOptions = cryRpt.ExportOptions
                With CrExportOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions

                End With
                cryRpt.Export()
                If SendMail(_empid.ToString, sFileName) Then
                    TNotification.MessageBox("Email Sent")
                End If

                If File.Exists(sFileLoc) Then
                    '  File.Delete(strFileLoc)
                End If
            End If
            Return True
        Catch ex As Exception
            Throw ex
        End Try 

1 个答案:

答案 0 :(得分:0)

你可以使用这样的东西:

       cryRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False,"ExportedReport")