vb 使用Cystal Reports 9小例子

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If sOption = 0 Then
            Try
                Dim f As New BogartMis.Bas.frmPrint
                Dim rpt As New ReportDocument
                Dim rptPath As String
                If chkCHNVER.Checked = True Then
                    rptpath = Application.StartupPath & "\NewReports\FG_CartonLableC.rpt"
                Else
                    rptpath = Application.StartupPath & "\NewReports\FG_CartonLable.rpt"
                End If
                rpt.Load(rptPath)
                Dim netDs As New DataSet
                Dim ctnid As String


                For Each R As DataRow In TmpT.Rows
                    If R("s_select") = 1 Then
                        If Len(ctnid) = 0 Then
                            ctnid = "'" & R("ctnid") & "'"
                        Else
                            ctnid = ctnid & ",'" & R("ctnid") & "'"
                        End If
                    End If

                Next


                Dim strCompyC As String
                If g.gLocation = LocationType.BRUNE Then
                    strCompyC = "Brunet"
                Else
                    If g.gLocation = LocationType.CHINA_SHENZHEN Then
                        strCompyC = "SHS"
                    Else
                        strCompyC = "Bogart"
                    End If

                End If


                Dim StrSql As String = "select a.ctnno,a.pkmeth,a.batchno,(select count(b.bobid) from FG_CartonBobbin b where b.ctnid=a.ctnid) TOTBOB,a.netwgtkg,a.cstpono,a.remark,a.grswgtkg,a.netwgtlb,a.deflenm,a.grslenm,a.netlenm,(case len(rtrim(dbo.FUN_FG_GET_CARTON_BANDNO(a.ctnid))) when 0 then c.deg else c.deg + '-' + dbo.FUN_FG_GET_CARTON_BANDNO(a.ctnid) end) as deg,c.olddeg,c.csprd,c.prddsc,c.cstord,c.clot,c.cordno,c.bicom,c.shcom,c.cscom,c.cstref,c.cscomd,c.prdcom,c.ordunm,c.spproc "
                StrSql += " from fg_cartondetail a left join dbo.FG_OrderDetail c on a.clot=c.clot and a.cstord=c.cstord and a.deg=c.deg and a.bicom=c.bicom and a.bandno=c.bandno "
                StrSql += " where a.clot='" & CLOT & "' and a.cstord='" + CSTORD + "' and a.deg='" + DEG + "' and a.bicom='" + BICOM + "' and a.bandno='" + BANDNO + "' and a.ctnid in (" & ctnid & ") "
                StrSql += " order by a.batchno,a.ctnno "

                Dim netPrint As DataTable = gData.GetDataTable(StrSql, sqlConn)

                AddBarcodeImages(netPrint)

                netDs.Tables.Add(netPrint)
                netDs.Tables(0).TableName = "Detail"

                Dim netLogo As OleDb.OleDbDataAdapter = gData.GetCompanyLogo(sqlConn, "", strCompyC)
                netLogo.Fill(netDs, "mos")

                rpt.SetDataSource(netDs)
                f.CrystalReportViewer1.ReportSource = rpt '傳數據給報表打印
                f.ShowDialog()

            Catch ex As Exception
                System.Console.WriteLine(ex.ToString)
            Finally
                gSub.setPrompt("Ready")
                Me.Cursor = Cursors.Default
            End Try
        Else
            Try


                Dim f As New BogartMis.Bas.frmPrint
                Dim rpt As New ReportDocument
                Dim rptPath As String
                If chkCHNVER.Checked = True Then
                    rptpath = Application.StartupPath & "\NewReports\FG_BobbinLableC.rpt"
                Else
                    rptpath = Application.StartupPath & "\NewReports\FG_BobbinLable.rpt"
                End If
                rpt.Load(rptPath)
                Dim netDs As New DataSet
                Dim bobid As String

                For Each R As DataRow In TmpT.Rows
                    If R("s_select") = 1 Then
                        If Len(bobid) = 0 Then
                            bobid = "'" & R("bobid") & "'"
                        Else
                            bobid = bobid & ",'" & R("bobid") & "'"
                        End If
                    End If

                Next


                Dim strCompyC As String
                If g.gLocation = LocationType.BRUNE Then
                    strCompyC = "Brunet"
                Else
                    If g.gLocation = LocationType.CHINA_SHENZHEN Then
                        strCompyC = "SHS"
                    Else
                        strCompyC = "Bogart"
                    End If

                End If

                Dim StrSql As String = "select '' as bobtyp,a.bobno,a.batchno,a.webno,a.grswgtkg,a.netwgtkg,a.netwgtlb,a.grslenm,a.deflenm,a.netlenm,a.remark,(case len(rtrim(c.bandno)) when 0 then c.deg else c.deg + '-' + c.bandno end) as deg,c.olddeg,c.cstord,c.cordno,c.clot,c.bicom,c.bandno,c.csprd,c.cstref,c.shcom,c.cscom,c.cscomd,c.ordunm,c.spproc "
                StrSql += " from fg_bobbindetail a left join dbo.FG_OrderDetail c on a.clot=c.clot and a.cstord=c.cstord and a.deg=c.deg and a.bicom=c.bicom and a.bandno=c.bandno "
                StrSql += " where a.clot='" & CLOT & "' and a.cstord='" + CSTORD + "' and a.deg='" + DEG + "' and a.bicom='" + BICOM + "' and a.bandno='" + BANDNO + "' and a.bobid in (" & bobid & ") order by bobno "

                Dim netPrint As DataTable = gData.GetDataTable(StrSql, sqlConn)
                netDs.Tables.Add(netPrint)
                netDs.Tables(0).TableName = "Detail"

                Dim netLogo As OleDb.OleDbDataAdapter = gData.GetCompanyLogo(sqlConn, "", strCompyC)
                netLogo.Fill(netDs, "mos")


                rpt.SetDataSource(netDs)
                f.CrystalReportViewer1.ReportSource = rpt
                f.ShowDialog()

            Catch ex As Exception
                System.Console.WriteLine(ex.ToString)
            Finally
                gSub.setPrompt("Ready")
                Me.Cursor = Cursors.Default
            End Try

        End If

    End Sub

    Private Sub AddBarcodeImages(ByVal DTab As DataTable)

        If Not DTab Is Nothing Then

            DTab.Columns.Add("BCIMAGE", Type.GetType("System.Byte[]"))

            Dim r As DataRow
            For Each r In DTab.Rows

                Try
                    Dim BatchNo As String = r.Item("batchno") & "-" & CStr(r.Item("ctnno")).Trim
                    Dim sFileName As String = "C:\TEMP\" & BatchNo & ".jpg"

                    Shell("Barcode.exe " & BatchNo & " 36", AppWinStyle.NormalFocus, True)'必須安裝.NET Framework v4.0以上版本
                    Dim fs As IO.FileStream = New IO.FileStream(sFileName, IO.FileMode.Open)
                    Dim fi As IO.FileInfo = New IO.FileInfo(sFileName)
                    Dim fl As Long = fi.Length
                    Dim lung As Integer = Convert.ToInt32(fl)
                    Dim imgBytes As Byte() = New Byte(lung - 1) {}
                    fs.Read(imgBytes, 0, lung)
                    fs.Close()
                    r.Item("BCIMAGE") = imgBytes

                    Dim f As IO.File
                    Try
                        f.Delete(sFileName)
                    Catch ex As Exception
                        Console.WriteLine(ex.ToString)
                    End Try

                Catch ex As Exception
                    ErrorMsg.Show(ex)
                End Try

            Next
        End If
    End Sub

vinson
原文地址:https://www.cnblogs.com/vinsonLu/p/2638136.html