Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlBordersIndex
Imports Microsoft.Office.Interop.Excel.XlLineStyle
Imports Microsoft.Office.Interop.Excel.XlBorderWeight
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Imports Microsoft.Office.Interop.Excel
Public Class 試穿_訂料授權書
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet : Dim ds1 As New DataSet : Dim ds2 As New DataSet
    Dim dr As SqlDataReader
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim N1 As Integer : Dim 位置1 As String
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.RowTemplate.Height = 50
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_清單()
        Set_DGV1載入前設定()
        If PA = "" Then
            SQL1 = "SELECT PAO, SO, 季節, 客戶, BUYER, 工廠, 型體號碼, 型體名稱, 總數量, ETD, ISSUE_DATE, FOB, ConstructionName, PS, 制單人, 制單時間, 版次 FROM 預告訂單控制表 ORDER BY PAO DESC, 版次 DESC"
        Else
            SQL1 = "SELECT PAO, SO, 季節, 客戶, BUYER, 工廠, 型體號碼, 型體名稱, 總數量, ETD, ISSUE_DATE, FOB, ConstructionName, PS, 制單人, 制單時間, 版次 FROM 預告訂單控制表 " &
                   "WHERE (SO LIKE N'%" & PA & "%') OR (季節 LIKE N'%" & PA & "%') OR (客戶 LIKE N'%" & PA & "%') OR (型體號碼 LIKE N'%" & PA & "%') OR (型體名稱 LIKE N'%" & PA & "%') " &
                   "OR (ISSUE_DATE LIKE N'%" & PA & "%') OR (ETD LIKE N'%" & PA & "%') OR (FOB LIKE N'%" & PA & "%') OR (ConstructionName LIKE N'%" & PA & "%') OR (PS LIKE N'%" & PA & "%') " &
                   "OR (制單人 LIKE N'%" & PA & "%') ORDER BY   PAO DESC, 版次 DESC"
        End If
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        If CheckBox1.Checked = False Then
            DataGridView1.Size = New Size(475, 944)
            DataGridView1.Columns(0).Width = 85 : DataGridView1.Columns(1).Width = 55 : DataGridView1.Columns(2).Width = 65 : DataGridView1.Columns(3).Width = 100 : DataGridView1.Columns(6).Width = 100
            DataGridView1.Columns(7).Visible = False
        Else
            DataGridView1.Size = New Size(745, 944)
            DataGridView1.Columns(0).Width = 85 : DataGridView1.Columns(1).Width = 55 : DataGridView1.Columns(2).Width = 65 : DataGridView1.Columns(3).Width = 170 : DataGridView1.Columns(6).Width = 150
            DataGridView1.Columns(7).Visible = True : DataGridView1.Columns(7).Width = 150
        End If
        DataGridView1.Columns(4).Visible = False : DataGridView1.Columns(5).Visible = False : DataGridView1.Columns(8).Visible = False
        DataGridView1.Columns(9).Visible = False : DataGridView1.Columns(10).Visible = False : DataGridView1.Columns(11).Visible = False : DataGridView1.Columns(12).Visible = False
        DataGridView1.Columns(13).Visible = False : DataGridView1.Columns(14).Visible = False : DataGridView1.Columns(15).Visible = False : DataGridView1.Columns(16).Width = 45
    End Sub
    Private Sub Set_DGV2載入前設定()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 40
        DataGridView2.AllowUserToAddRows = False
        DataGridView2.RowTemplate.Height = 150
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV2載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid1()
    End Sub
    Private Sub Set_清單1()
        Set_DGV2載入前設定()
        SQL1 = "SELECT 預告訂單控制表.PAO, 預告訂單控制表.版次, 預告訂單控制表.季節, 預告訂單控制表.客戶, 
                       預告訂單控制表.型體名稱 AS STYLE_NAME, 預告訂單清單.顏色 AS COLOR, 
                       預告訂單清單.顏色 AS MATERIAL_DESCRIPTION, 預告訂單控制表.ETD AS XF, 
                       預告訂單控制表.SO AS [REFERENCE SAMPL], 預告訂單清單.FITTING_COMMENTS AS [FITTING COMMENTS], 
                       預告訂單清單.MATERAIL_COMMENTS AS [MATERAL COMMENTS], 預告訂單清單.FITTING回簽確認, 預告訂單清單.FITTING回簽日期, 
                       預告訂單清單.MATERAIL回簽確認 AS MATERAL回簽確認, 預告訂單清單.MATERAIL回簽日期 AS MATERAL回簽日期, 預告訂單清單.流水號, 預告訂單控制表.型體號碼
                FROM 預告訂單控制表 INNER JOIN
                     預告訂單清單 ON 預告訂單控制表.PAO = 預告訂單清單.PAO AND 預告訂單控制表.版次 = 預告訂單清單.版次
                WHERE (預告訂單控制表.PAO LIKE '" & TextBox1.Text & "') AND (預告訂單控制表.版次 LIKE '" & TextBox2.Text & "')
                GROUP BY 預告訂單控制表.PAO, 預告訂單控制表.版次, 預告訂單控制表.SO, 預告訂單控制表.季節, 預告訂單控制表.客戶, 
                         預告訂單清單.顏色, 預告訂單控制表.ETD, 預告訂單控制表.型體名稱, 預告訂單清單.FITTING_COMMENTS, 
                         預告訂單清單.MATERAIL_COMMENTS, 預告訂單清單.FITTING回簽確認, 預告訂單清單.FITTING回簽日期, 
                         預告訂單清單.MATERAIL回簽確認, 預告訂單清單.MATERAIL回簽日期, 預告訂單清單.流水號, 預告訂單控制表.型體號碼"
        Set_DGV2載入後設定()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Visible = False : DataGridView2.Columns(1).Visible = False : DataGridView2.Columns(2).Visible = False : DataGridView2.Columns(3).Width = 100
        DataGridView2.Columns(4).Width = 100 : DataGridView2.Columns(5).Width = 100 : DataGridView2.Columns(6).Width = 275 : DataGridView2.Columns(7).Width = 80 : DataGridView2.Columns(8).Width = 80
        DataGridView2.Columns(9).Width = 125 : DataGridView2.Columns(10).Width = 125 : DataGridView2.Columns(11).Width = 100 : DataGridView2.Columns(12).Width = 100 : DataGridView2.Columns(13).Width = 100
        DataGridView2.Columns(14).Width = 100 : DataGridView2.Columns(15).Visible = False : DataGridView2.Columns(16).Visible = False
        DataGridView2.EditMode = DataGridViewEditMode.EditOnEnter
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            資料數 = DataGridView2.Rows.Count : MyModule1.進度條()
            DataGridView2.Rows(i).Cells("MATERIAL_DESCRIPTION").Value = ""
            Dim aa As Integer = 1
            For ii As Integer = 0 To DataGridView3.Rows.Count - 1
                If DataGridView2.Rows(i).Cells("流水號").Value = DataGridView3.Rows(ii).Cells("流水號").Value Then
                    If aa = 1 Then
                        DataGridView2.Rows(i).Cells("MATERIAL_DESCRIPTION").Value = aa & "、" & DataGridView3.Rows(ii).Cells("材料名稱").Value & "-" & DataGridView3.Rows(ii).Cells("材料說明").Value
                        aa += 1
                    Else
                        DataGridView2.Rows(i).Cells("MATERIAL_DESCRIPTION").Value = DataGridView2.Rows(i).Cells("MATERIAL_DESCRIPTION").Value & " 

" & aa & "、" & DataGridView3.Rows(ii).Cells("材料名稱").Value & "-" & DataGridView3.Rows(ii).Cells("材料說明").Value
                        aa += 1
                    End If
                End If
            Next
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub Set_DGV3載入前設定()
        DataGridView3.DataSource = Nothing : ds2.Clear()
        DataGridView3.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView3.ColumnHeadersHeight = 25
        DataGridView3.AllowUserToAddRows = False
        DataGridView3.RowTemplate.Height = 50
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV3載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView3.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單2()
        Set_DGV3載入前設定()
        SQL1 = "SELECT 預告訂單清單.PAO, 預告訂單部件表.部件, 預告訂單部件表.材料名稱, 預告訂單部件表.材料說明, 
                       預告訂單部件表.備註, 預告訂單部件表.流水號
                FROM 預告訂單清單 INNER JOIN
                     預告訂單部件表 ON 預告訂單清單.PAO = 預告訂單部件表.PAO AND 
                     預告訂單清單.版次 = 預告訂單部件表.版次
                WHERE (預告訂單清單.PAO LIKE '" & TextBox1.Text & "') AND (預告訂單清單.版次 LIKE '" & TextBox2.Text & "') AND (預告訂單部件表.部件 LIKE 'A%')
                GROUP BY 預告訂單清單.PAO, 預告訂單部件表.部件, 預告訂單部件表.材料名稱, 預告訂單部件表.材料說明, 
                            預告訂單部件表.備註, 預告訂單部件表.流水號"
        Set_DGV3載入後設定()
    End Sub
    Private Sub 試穿_定料授權書_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        DataGridView3.Visible = False
        Set_清單()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Label62.Visible = True Then : Label62.Visible = False : Else : Label62.Visible = True : End If
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("PAO").Value : TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("版次").Value : Set_清單2() : Set_清單1()
            TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("客戶").Value : Timer1.Enabled = False
            PictureBox2.Image = Nothing : conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
           "WHERE 客戶 LIKE N'" & DataGridView1.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            While dr.Read() = True
                Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox2.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close() : PictureBox2.SizeMode = 4 : PictureBox1.Image = Nothing
        End If
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            Timer1.Enabled = True
            PictureBox1.Image = Nothing : conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                       "WHERE 客戶 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("型體號碼").Value &
                       "' AND 形體名 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("STYLE_NAME").Value & "' AND 顏色 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("COLOR").Value & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                PictureBox1.Image = Nothing : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                       "WHERE 客戶 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("型體號碼").Value &
                       "' AND 形體名 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("STYLE_NAME").Value & "' AND 顏色 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("COLOR").Value & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                While dr.Read() = True
                    Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                End While
                conn.Close()
            Else
                PictureBox1.Image = Nothing : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                   "WHERE 客戶 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then
                    PictureBox1.Image = Nothing : conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                   "WHERE 客戶 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    While dr.Read() = True
                        Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                Else
                    PictureBox1.Image = Nothing : conn.Close()
                End If
                conn.Close()
            End If : PictureBox1.SizeMode = 4
        End If
    End Sub
    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_清單() : PA = ""
    End Sub
    Private Sub Button32_Click(sender As Object, e As EventArgs) Handles Button32.Click
        PA = "" : Set_清單()
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
        Set_grid()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            資料數 = DataGridView2.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE  預告訂單清單 SET FITTING_COMMENTS = N'" & DataGridView2.Rows(i).Cells("FITTING COMMENTS").Value &
                                         "',MATERAIL_COMMENTS = N'" & DataGridView2.Rows(i).Cells("MATERAL COMMENTS").Value &
                                          "', FITTING回簽確認 = N'" & DataGridView2.Rows(i).Cells("FITTING回簽確認").Value &
                                          "', FITTING回簽日期 = N'" & DataGridView2.Rows(i).Cells("FITTING回簽日期").Value &
                                         "', MATERAIL回簽確認 = N'" & DataGridView2.Rows(i).Cells("MATERAIL回簽確認").Value &
                                         "', MATERAIL回簽日期 = N'" & DataGridView2.Rows(i).Cells("MATERAIL回簽日期").Value &
                                         "' WHERE 流水號 LIKE N'" & DataGridView2.Rows(i).Cells("流水號").Value & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        Timer1.Enabled = False : Set_清單1() : MsgBox("存檔完成。")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        xlApp = CType(CreateObject("Excel.Application"), Application)
        xlBook = xlApp.Workbooks.Add
        xlApp.DisplayAlerts = True
        xlBook.Activate()
        xlSheet = NewMethod(xlBook)
        xlSheet.Activate()
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized

        xlSheet.Cells(1, 1) = "FIT MATERIAL AUTHORIZATION FORM" : xlSheet.Cells(2, 1) = TextBox3.Text : xlSheet.Cells(3, 1) = "DATE : " & Strings.Format(Today(), "yyyy/MM/dd")
        xlSheet.Cells(4, 1) = "PHOTO" : xlSheet.Cells(4, 2) = "BUYER" : xlSheet.Cells(4, 3) = "STYLE
NAME"
        xlSheet.Cells(4, 4) = "COLOR" : xlSheet.Cells(4, 5) = " MATERIAL
DESCRIPTION"
        xlSheet.Cells(4, 6) = "XF" : xlSheet.Cells(4, 7) = "REFERENCE 
SAMPLE"
        xlSheet.Cells(4, 8) = "FITTING
COMMENTS"
        xlSheet.Cells(4, 9) = "MATERAL 
COMMENTS"

        N1 = 0
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            xlSheet.Cells(i + 5, 2) = DataGridView2.Rows(i).Cells("客戶").Value
            xlSheet.Cells(i + 5, 3) = DataGridView2.Rows(i).Cells("STYLE_NAME").Value
            xlSheet.Cells(i + 5, 4) = DataGridView2.Rows(i).Cells("COLOR").Value
            xlSheet.Cells(i + 5, 5) = DataGridView2.Rows(i).Cells("MATERIAL_DESCRIPTION").Value
            xlSheet.Cells(i + 5, 6) = DataGridView2.Rows(i).Cells("XF").Value
            xlSheet.Cells(i + 5, 7) = DataGridView2.Rows(i).Cells("REFERENCE SAMPL").Value
            xlSheet.Cells(i + 5, 8) = DataGridView2.Rows(i).Cells("FITTING COMMENTS").Value
            xlSheet.Cells(i + 5, 9) = DataGridView2.Rows(i).Cells("MATERAL COMMENTS").Value
            N1 = i + 5

            PictureBox1.Image = Nothing : conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                   "WHERE 客戶 LIKE N'" & DataGridView2.Rows(i).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(i).Cells("型體號碼").Value &
                   "' AND 形體名 LIKE N'" & DataGridView2.Rows(i).Cells("STYLE_NAME").Value & "' AND 顏色 LIKE N'" & DataGridView2.Rows(i).Cells("COLOR").Value & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                PictureBox1.Image = Nothing : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                   "WHERE 客戶 LIKE N'" & DataGridView2.Rows(i).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(i).Cells("型體號碼").Value &
                   "' AND 形體名 LIKE N'" & DataGridView2.Rows(i).Cells("STYLE_NAME").Value & "' AND 顏色 LIKE N'" & DataGridView2.Rows(i).Cells("COLOR").Value & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                While dr.Read() = True
                    Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                End While
                conn.Close() : GG(xlApp, xlSheet)
            Else
                PictureBox1.Image = Nothing : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                   "WHERE 客戶 LIKE N'" & DataGridView2.Rows(i).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(i).Cells("型體號碼").Value & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then
                    PictureBox1.Image = Nothing : conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                   "WHERE 客戶 LIKE N'" & DataGridView2.Rows(i).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView2.Rows(i).Cells("型體號碼").Value & "'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    While dr.Read() = True
                        Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close() : GG(xlApp, xlSheet)
                Else
                    xlSheet.Cells(i + 5, 1) = "系統中無圖片"
                    conn.Close()
                End If
            End If : PictureBox1.SizeMode = 4
        Next i
        BB(xlApp, xlSheet)

        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        MsgBox("列印完成")
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With

        xlSheet.Rows("1:1").Select
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 28 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With

        xlSheet.Rows("2:2").Select
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 16 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With

        xlSheet.Rows("3:3").Select
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 16 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With

        xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 12 : xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 15
        xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 12 : xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 15
        xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 60 : xlSheet.Columns("F:F").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("G:G").Select : myExcel.Selection.ColumnWidth = 15 : xlSheet.Columns("H:H").Select : myExcel.Selection.ColumnWidth = 20
        xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 20 : xlSheet.Rows("4:4").Select : myExcel.Selection.RowHeight = 40 : myExcel.Selection.Font.Bold = True

        For i As Integer = 5 To N1
            'xlSheet.Rows(i & ":" & i).Select : myExcel.Rows(i & ":" & i).EntireRow.AutoFit
            xlSheet.Rows(i & ":" & i).Select : myExcel.Selection.RowHeight = 80
        Next

        xlSheet.Columns("F:F").Select
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "A4:I" & N1 : xlSheet.Range(位置1).Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Rows("5:5").Select : myExcel.ActiveWindow.FreezePanes = True : myExcel.ActiveWindow.DisplayGridlines = False
        xlSheet.Range("F1").Select()

        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        myExcel.Application.PrintCommunication = True
        myExcel.ActiveSheet.PageSetup.PrintArea = ""
        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
            .LeftMargin = myExcel.Application.InchesToPoints(0.25)
            .RightMargin = myExcel.Application.InchesToPoints(0.25)
            .TopMargin = myExcel.Application.InchesToPoints(0.75)
            .BottomMargin = myExcel.Application.InchesToPoints(0.75)
            .HeaderMargin = myExcel.Application.InchesToPoints(0.3)
            .FooterMargin = myExcel.Application.InchesToPoints(0.3)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintQuality = 600
            .CenterHorizontally = False
            .CenterVertically = False
            .Draft = False
            .FirstPageNumber = xlAutomatic
            .BlackAndWhite = False
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = ""
            .EvenPage.RightHeader.Text = ""
            .EvenPage.LeftFooter.Text = ""
            .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = ""
            .FirstPage.CenterHeader.Text = ""
            .FirstPage.RightHeader.Text = ""
            .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
        myExcel.Application.PrintCommunication = True
    End Sub
    Private Sub GG(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
        Dim 位置1 As String
        Clipboard.SetDataObject(PictureBox1.Image)
        位置1 = "A" & N1 : xlSheet.Range(位置1).Select()
        myExcel.ActiveSheet.Pictures.Paste.Select
        myExcel.Selection.ShapeRange.Width = 70
        myExcel.Selection.ShapeRange.IncrementLeft(3)
        myExcel.Selection.ShapeRange.IncrementTop(3)
    End Sub
End Class