为VB.net中的每个客户创建PDF文档

时间:2017-05-22 19:16:29

标签: sql vb.net visual-studio pdf crystal-reports

我需要为每个客户创建一个PDF文件,该文件是从SQL查询中选择的结果。

例如,"客户1"该客户的所有信息都应该转到名为" Customer1.pdf"的文件中。然后"客户2"所有信息都包含在" Customer2.pdf"等等,直到SQL查询中的最后一个客户。

代码有效,问题是为每个客户创建每个文档的循环:它只创建包含所有信息的一个文档。

我的VB.net代码示例:

Dim customer_SQL As String

Using connObj As New SqlConnection(sql conection)
    Using cmdObj As New SqlClient.SqlCommand("Select Distinct(customer_ID) from my table  Where custmerid is not null", connObj)
        connObj.Open()
        Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
            ' This will loop through all returned records '
            While readerObj.Read
                Customer_SQL = readerObj("Customer_ID").ToString
                ' To see if it return the value I want
                'MessageBox.Show(Customer_SQL.ToString)

                Try
                    Dim CrExp As ExportOptions
                    Dim CrDiskFileDest As New DiskFileDestinationOptions()
                    Dim crFormatTypeopt As New PdfRtfWordFormatOptions()
                    For Li_count As Integer = 0 To Customer_SQL
                        CrDiskFileDest.DiskFileName = "C:\Report" & Customer_SQL.ToString & ".pdf"
                        CrExp = cryRpt.ExportOptions
                        With CrExp
                            .ExportDestinationType = ExportDestinationType.DiskFile
                            .ExportFormatType = ExportFormatType.PortableDocFormat
                            .DestinationOptions = CrDiskFileDest
                            .FormatOptions = crFormatTypeopt

                        End With
                        cryRpt.Export()
                    Next
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End While
        End Using
        connObj.Close()
    End Using
End Using

2 个答案:

答案 0 :(得分:0)

 Using connObj As New SqlConnection(sql conection)
    Using cmdObj As New SqlClient.SqlCommand("Select Distinct(customer_ID) from my table  Where custmerid is not null, connObj)
            connObj.Open()
            Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
                'This will loop through all returned records 
                While readerObj.Read
                    Customer_SQL = readerObj("Customer_ID").ToString
                    ' To see if it return the value I want
                    'MessageBox.Show(Customer_SQL.ToString)

                    Try
                        Dim CrExp As ExportOptions
                        Dim CrDiskFileDest As New DiskFileDestinationOptions()
                        Dim crFormatTypeopt As New PdfRtfWordFormatOptions()
                        For Li_count As Integer = 0 To Customer_SQL

                            CrDiskFileDest.DiskFileName = "C:\Report" & Li_count & ".pdf"

                            CrExp = cryRpt.ExportOptions
                            With CrExp
                                .ExportDestinationType = ExportDestinationType.DiskFile
                                .ExportFormatType = ExportFormatType.PortableDocFormat
                                .DestinationOptions = CrDiskFileDest
                                .FormatOptions = crFormatTypeopt

                            End With
                            cryRpt.Export()
                        Next
                    Catch ex As Exception
                        MsgBox(ex.ToString)
                    End Try
                End While
            End Using
            connObj.Close()
        End Using
    End Using
End Sub

我认为错误发生在第16行

答案 1 :(得分:0)

我解决了这个问题,谢谢大家的帮助: 此外,如果有人需要使用Crystal Report从Visual Studio中删除PDF文件或任何文件..这就是Firmula

Dim Customer_id As String
        Using connObj As New SqlConnection("SQL conection")
            Using cmdObj As New SqlClient.SqlCommand("Select Distinct(Customer_ID) from Customer_DIM", connObj)
                connObj.Open()
                Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
                    'This will loop through all returned records 
                    While readerObj.Read
                        Customer_id = readerObj("Customer_ID").ToString
                        'Parameters
                        cryRpt.SetParameterValue("Prompts_Customer_id", Customer_id)

                        Dim List As New List(Of String) From {Customer_id}

                        'handle returned value before next loop here

                        Try
                            Dim CrExp As ExportOptions
                            Dim CrDiskFileDest As New DiskFileDestinationOptions()
                            Dim crFormatTypeopt As New PdfRtfWordFormatOptions()
                            For Each Customer_id In List
                                CrDiskFileDest.DiskFileName = "C:\Report\R" & Customer_id & ".pdf"
                                CrExp = cryRpt.ExportOptions
                                With CrExp
                                    .ExportDestinationType = ExportDestinationType.DiskFile
                                    .ExportFormatType = ExportFormatType.PortableDocFormat
                                    .DestinationOptions = CrDiskFileDest
                                    .FormatOptions = crFormatTypeopt

                                End With
                                cryRpt.Export()
                            Next Customer_id
                        Catch ex As Exception
                            MsgBox(ex.ToString)
                        End Try
                    End While
                End Using
                connObj.Close()
            End Using
        End Using
    End Sub