在按钮单击上加载Crystal报表并导出为PDF [英] Load Crystal Report and Export to PDF on Button Click

查看:63
本文介绍了在按钮单击上加载Crystal报表并导出为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将晶体报告加载到水晶报告查看器中,并在此站点上找到代码。

I am loading a crystal report into a crystal report viewer with code on found on this site.

Dim objReport As New ReportDocument()<
Friend Function ViewReport(ByVal sReportName As String, Optional ByVal sSelectionFormula As String = "", Optional ByVal param As String = "") As Boolean
        'http://www.codeproject.com/KB/recipes/CrystalReports_in_VBNET.aspx

        Dim intCounter As Integer
        Dim intCounter1 As Integer

        'Crystal Report's report document object
        Dim objReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument()

        'object of table Log on info of Crystal report
        Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo

        'Parameter value object of crystal report 
        ' parameters used for adding the value to parameter.
        Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue

        'Current parameter value object(collection) of crystal report parameters.
        Dim currValue As CrystalDecisions.Shared.ParameterValues

        'Sub report object of crystal report.
        Dim mySubReportObject As  _
            CrystalDecisions.CrystalReports.Engine.SubreportObject

        'Sub report document of crystal report.
        Dim mySubRepDoc As New  _
            CrystalDecisions.CrystalReports.Engine.ReportDocument

        Dim strParValPair() As String
        Dim strVal() As String
        Dim index As Integer

        Try

            'Load the report
            objReport.Load(sReportName)

            'Check if there are parameters or not in report.
            intCounter = objReport.DataDefinition.ParameterFields.Count

            'As parameter fields collection also picks the selection 
            ' formula which is not the parameter
            ' so if total parameter count is 1 then we check whether 
            ' its a parameter or selection formula.

            If intCounter = 1 Then
                If InStr(objReport.DataDefinition.ParameterFields(0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
                    intCounter = 0
                End If
            End If

            'If there are parameters in report and 
            'user has passed them then split the 
            'parameter string and Apply the values 
            'to their concurrent parameters.

            If intCounter > 0 And Trim(param) <> "" Then
                strParValPair = param.Split("&")

                For index = 0 To UBound(strParValPair)
                    If InStr(strParValPair(index), "=") > 0 Then
                        strVal = strParValPair(index).Split("=")
                        paraValue.Value = strVal(1)
                        currValue = _
                            objReport.DataDefinition.ParameterFields( _
                            strVal(0)).CurrentValues
                        currValue.Add(paraValue)
                        objReport.DataDefinition.ParameterFields( _
                    strVal(0)).ApplyCurrentValues(currValue)
                    End If
                Next

            End If

            'Set the connection information to ConInfo 
            'object so that we can apply the 
            'connection information on each table in the report
            ConInfo.ConnectionInfo.IntegratedSecurity = True
            ConInfo.ConnectionInfo.ServerName = "192.168.160.54"
            ConInfo.ConnectionInfo.DatabaseName = "ProviderDesktop"

            For intCounter = 0 To objReport.Database.Tables.Count - 1
                objReport.Database.Tables(intCounter).ApplyLogOnInfo(ConInfo)
            Next

            ' Loop through each section on the report then look 
            ' through each object in the section
            ' if the object is a subreport, then apply logon info 
            ' on each table of that sub report

            For index = 0 To objReport.ReportDefinition.Sections.Count - 1
                For intCounter = 0 To _
                    objReport.ReportDefinition.Sections(index).ReportObjects.Count - 1
                    With objReport.ReportDefinition.Sections(index)
                        If .ReportObjects(intCounter).Kind = _
                        CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
                            mySubReportObject = CType(.ReportObjects(intCounter),  _
                              CrystalDecisions.CrystalReports.Engine.SubreportObject)
                            mySubRepDoc = _
                     mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
                            For intCounter1 = 0 To mySubRepDoc.Database.Tables.Count - 1
                                mySubRepDoc.Database.Tables( _
                                    intCounter1).ApplyLogOnInfo( _
                                    ConInfo)
                                mySubRepDoc.Database.Tables( _
                           intCounter1).ApplyLogOnInfo(ConInfo)
                            Next

                        End If
                    End With
                Next
            Next
            'If there is a selection formula passed to this function then use that
            If sSelectionFormula.Length > 0 Then
                objReport.RecordSelectionFormula = sSelectionFormula
            End If
            'Re setting control 
            crvViewer.ReportSource = Nothing

            'Set the current report object to report.
            crvViewer.ReportSource = objReport

            'Show the report
            crvViewer.Show()

            Return True
        Catch ex As System.Exception
            MessageBox.Show("ViewReport: " & vbCrLf & ex.Message, "Provider Desktop Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function





然后,我想获取水晶报表查看器中加载的报表并将其导出为PDF。





I would then like to take the report that is loaded in the crystal report viewer and export it to PDF.

Private Sub IndexDocument(ByVal frmClose As Boolean)
        note_path = "C:\test123.pdf"

        Dim CrExportOptions As ExportOptions
        Dim CrDiskFileDestinationOptions As New  _
        DiskFileDestinationOptions()
        Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
        CrDiskFileDestinationOptions.DiskFileName = note_path
        CrExportOptions = objReport.ExportOptions
        With CrExportOptions
            .ExportDestinationType = ExportDestinationType.DiskFile
            .ExportFormatType = ExportFormatType.PortableDocFormat
            .DestinationOptions = CrDiskFileDestinationOptions
            .FormatOptions = CrFormatTypeOptions
        End With
        objReport.Export()

objReport.Close()
        objReport.Dispose()

        If frmClose = True Then
            Me.Close()
        End If
                'End Try
    End Sub





我在行CrExportOptions = objReport.ExportOptions上收到无效的报告路径错误。我在其他表单上有这个代码,我没有这个问题。谁能指出我正确的方向?



谢谢,



克里斯汀



I am getting an "invalid report path" error on line CrExportOptions = objReport.ExportOptions. I have this code on other forms and I do not have this issue. Can anyone point me in the right direction?

Thanks,

Kristen

推荐答案

没关系......我不得不拿出这条线



Dim objReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument ()


ViewReport函数下的
Never mind... I had to take out the line

Dim objReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument()

under the ViewReport function.


这篇关于在按钮单击上加载Crystal报表并导出为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆