Option Strict Off
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
Public Class 採購對帳單
    ReadOnly ds, ds1, ds2, ds3, ds4, ds5 As New DataSet
    Dim EDR As Integer = 0 : Dim ESTR As String : Dim 關鍵字, 首次開啟 As Boolean
    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 N2 As Integer
    Private Sub Set_清單1()
        採購清單_dgv.DataSource = Nothing : ds1.Clear()
        採購清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        採購清單_dgv.ColumnHeadersHeight = 40
        採購清單_dgv.AllowUserToAddRows = False

        If 小間距_ch.Checked = False And 中間距_ch.Checked = False And 大間距_ch.Checked = True Then : 採購清單_dgv.RowTemplate.Height = 95
        ElseIf 小間距_ch.Checked = False And 中間距_ch.Checked = True And 大間距_ch.Checked = False Then : 採購清單_dgv.RowTemplate.Height = 60
        ElseIf 小間距_ch.Checked = True And 中間距_ch.Checked = False And 大間距_ch.Checked = False Then : 採購清單_dgv.RowTemplate.Height = 25 : End If

        If 關鍵字 = True Then : Else : PA = 供應商編碼1_cb.Text : End If
        PA20 = 業務1_cb.Text
        If 未過帳_ch.Checked = True Then
            SQL_採購控制表_未過帳()
        Else
            SQL_採購控制表_已過帳()
        End If
        da.Fill(ds1) : 採購清單_dgv.DataSource = ds1.Tables(0) : conn.Close()
        Set_採購清單_dgv_下拉式清單1() : Set_採購清單_dgv_下拉式清單2() : Set_採購清單_dgv_下拉式清單3()
        採購清單_dgv.Columns(0).FillWeight = 40 : 採購清單_dgv.Columns(1).Visible = False : 採購清單_dgv.Columns(2).FillWeight = 70 : 採購清單_dgv.Columns(3).Visible = False
        採購清單_dgv.Columns(4).FillWeight = 80 : 採購清單_dgv.Columns(5).FillWeight = 80 : 採購清單_dgv.Columns(6).FillWeight = 200 : 採購清單_dgv.Columns(7).Visible = False
        採購清單_dgv.Columns(8).FillWeight = 100 : 採購清單_dgv.Columns(9).Visible = False : 採購清單_dgv.Columns(10).Visible = False : 採購清單_dgv.Columns(11).FillWeight = 160
        採購清單_dgv.Columns(12).Visible = False : 採購清單_dgv.Columns(13).Visible = False : 採購清單_dgv.Columns(14).FillWeight = 95 : 採購清單_dgv.Columns(15).Visible = False
        採購清單_dgv.Columns(16).FillWeight = 195 : 採購清單_dgv.Columns(17).Visible = False : 採購清單_dgv.Columns(18).FillWeight = 45 : 採購清單_dgv.Columns(19).FillWeight = 80
        採購清單_dgv.Columns(20).Visible = False : 採購清單_dgv.Columns(21).Visible = False : 採購清單_dgv.Columns(22).Visible = False : 採購清單_dgv.Columns(23).FillWeight = 80
        採購清單_dgv.Columns(24).FillWeight = 80 : 採購清單_dgv.Columns(25).FillWeight = 80 : 採購清單_dgv.Columns(26).FillWeight = 80 : 採購清單_dgv.Columns(27).FillWeight = 45
        採購清單_dgv.Columns(28).FillWeight = 45 : 採購清單_dgv.Columns(29).Visible = False
        採購清單_dgv.Columns("數量").DefaultCellStyle.Format = "#,##0" : 採購清單_dgv.Columns("成本").DefaultCellStyle.Format = "#,##0.0"
        採購清單_dgv.Columns("金額").DefaultCellStyle.Format = "#,##0"
        採購清單_dgv.Columns("數量").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("數量").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("成本").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("成本").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("金額").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("金額").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns(0).ReadOnly = True : 採購清單_dgv.Columns(2).ReadOnly = True : 採購清單_dgv.Columns(21).ReadOnly = True
        採購清單_dgv.Columns(4).ReadOnly = True : 採購清單_dgv.Columns(5).ReadOnly = True : 採購清單_dgv.Columns(6).ReadOnly = True
        採購清單_dgv.Columns(22).ReadOnly = True : 採購清單_dgv.Columns(17).ReadOnly = True : 採購清單_dgv.Columns(20).ReadOnly = True
        採購清單_dgv.EditMode = DataGridViewEditMode.EditOnEnter : 資料筆數1_tb.Text = 採購清單_dgv.Rows.Count
        關鍵字 = False
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            採購清單_dgv("選擇", i).Value = False
        Next
        Set_合記計算() : Set_清單4()
    End Sub
    Private Sub Set_採購清單_dgv_下拉式清單1()
        Dim Col As New DataGridViewComboBoxColumn With {
            .FillWeight = 130,
            .DataPropertyName = "倉名"
        }
        SQL_倉名_下拉清單()
        Col.Items.Clear()
        While (dr.Read()) : Col.Items.Add(dr("倉名")) : End While : conn.Close()
        Col.HeaderText = "倉名" : Col.Name = "倉名"
        採購清單_dgv.Columns.Insert(8, Col)
    End Sub
    Private Sub Set_採購清單_dgv_下拉式清單2()
        Dim Col As New DataGridViewComboBoxColumn With {
            .FillWeight = 130,
            .DataPropertyName = "訂製"
        }
        SQL_訂製狀態_下拉清單()
        Col.Items.Clear()
        While (dr.Read()) : Col.Items.Add(dr("訂製")) : End While : conn.Close()
        Col.HeaderText = "訂製" : Col.Name = "訂製"
        採購清單_dgv.Columns.Insert(11, Col)
    End Sub
    Private Sub Set_採購清單_dgv_下拉式清單3()
        Dim Col As New DataGridViewComboBoxColumn With {
            .FillWeight = 130,
            .DataPropertyName = "狀態"
        }
        SQL_倉儲狀態_下拉清單()
        Col.Items.Clear()
        While (dr.Read()) : Col.Items.Add(dr("狀態")) : End While : conn.Close()
        Col.HeaderText = "狀態" : Col.Name = "狀態"
        採購清單_dgv.Columns.Insert(14, Col)
    End Sub
    Private Sub Set_合記計算()
        採購金額_tb.Text = "0" : 採購金額1_tb.Text = "0"
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            採購清單_dgv("金額", i).Value = 採購清單_dgv("數量", i).Value * 採購清單_dgv("成本", i).Value
            採購金額_tb.Text = Val(採購金額_tb.Text) + 採購清單_dgv("金額", i).Value

            If 供應商_tb.Text = 採購清單_dgv("供應商", i).Value.ToString Then
                採購金額1_tb.Text = Val(採購金額1_tb.Text) + 採購清單_dgv("金額", i).Value
            End If
        Next
        採購金額_tb.Text = Strings.Format(Val(採購金額_tb.Text), "#,##0") : 採購金額1_tb.Text = Strings.Format(Val(採購金額1_tb.Text), "#,##0")
    End Sub
    Private Sub Set_清單4()
        外埔報表_dgv.DataSource = Nothing : ds5.Clear()
        外埔報表_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        外埔報表_dgv.ColumnHeadersHeight = 25
        外埔報表_dgv.AllowUserToAddRows = False
        外埔報表_dgv.RowTemplate.Height = 25
        PA = 供應商編碼1_cb.Text
        If 未過帳_ch.Checked = True Then
            SQL_採購控制表_外埔報表()
        Else
            SQL_採購控制表_外埔報表_已過()
        End If

        da.Fill(ds5) : 外埔報表_dgv.DataSource = ds5.Tables(0) : conn.Close()
        外埔報表_dgv.Columns(0).Visible = False : 外埔報表_dgv.Columns(1).FillWeight = 100 : 外埔報表_dgv.Columns(2).FillWeight = 250 : 外埔報表_dgv.Columns(3).FillWeight = 80
        外埔報表_dgv.Columns(4).FillWeight = 90 : 外埔報表_dgv.Columns(5).Visible = False : 外埔報表_dgv.Columns(6).Visible = False
        外埔報表_dgv.Columns("金額").DefaultCellStyle.Format = "#,##0"
        外埔報表_dgv.Columns("金額").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    End Sub
    Private Sub Set_清單5()
        訂製圖檔_dgv.DataSource = Nothing : ds2.Clear()
        訂製圖檔_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        訂製圖檔_dgv.ColumnHeadersHeight = 25
        訂製圖檔_dgv.AllowUserToAddRows = False
        訂製圖檔_dgv.RowTemplate.Height = 25

        SQL_採購控制表_訂製圖檔清單()
        da.Fill(ds2) : 訂製圖檔_dgv.DataSource = ds2.Tables(0) : conn.Close()
        訂製圖檔_dgv.Columns(0).Visible = False : 訂製圖檔_dgv.Columns(1).Visible = False : 訂製圖檔_dgv.Columns(2).FillWeight = 100
    End Sub
    Private Sub 供應商1_下拉表單資料載入()
        BL1 = False : PA3 = "" : PA4 = ""
        If 地區1_cb.Text = "" Then : PA3 = ""
        ElseIf 地區1_cb.Text = "北部" Then : PA3 = "2"
        ElseIf 地區1_cb.Text = "南部" Then : PA3 = "3"
        ElseIf 地區1_cb.Text = "材料" Then : PA3 = "9"
        ElseIf 地區1_cb.Text = "調貨" Then : PA3 = "Y"
        End If
        SQL_盤盈盤損申請_供應商_下拉清單() : 供應商1_cb.Items.Clear() : 供應商編碼1_cb.Items.Clear()
        While (dr.Read()) : 供應商1_cb.Items.Add(dr("簡稱")) : 供應商編碼1_cb.Items.Add(dr("編號")) : End While : conn.Close()
    End Sub
    Private Sub 業務_cb_下拉表單資料載入()
        SQL_業務編號_下拉清單() : 業務1_cb.Items.Clear() : 業務1_cb.Items.Clear()
        While (dr.Read()) : 業務1_cb.Items.Add(dr("業務號")) : End While : conn.Close()
    End Sub
    Private Sub 供應商_下拉表單資料載入()
        SQL_採購控制表_供應商_下拉清單() : 供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear()
        While (dr.Read()) : 供應商_cb.Items.Add(dr("簡稱")) : 供應商編碼_cb.Items.Add(dr("編號")) : End While : conn.Close()
    End Sub
    Private Sub 採購控制表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YD_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True : Panel1.SendToBack()
        Dim type0 As Type = 採購清單_dgv.GetType()
        Dim pi0 As Reflection.PropertyInfo = type0.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
        pi0.SetValue(採購清單_dgv, True, Nothing)
        小間距_ch.Checked = True   '--------------------控件隱藏、操作許可設定與其他基本顯示設定
        業務_cb_下拉表單資料載入() : 供應商_下拉表單資料載入()
        首次開啟 = True : Set_清單1() : 首次開啟 = False
        Target1 = Target & AA(1) & "/" : FTP資料夾_wb.Url = New Uri(Target1) '--------------------指定伺服器ftp資料夾位置
        說明_lb.Text = "注意實際進廠日，已經有日期!!" & vbCrLf & "會自動帶入過帳日期。如果需要手動修改，請記得要先存檔再按過帳。"
        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 7.6
        End If
        MsgBox("請先確認預設過帳日期!!")
        已確認_ch.Checked = True
    End Sub
    Private Sub 一般性申請採購單_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        If 首次開啟 = True Then
            If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
                字體_NUD.Value = 9
            ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
                字體_NUD.Value = 7.6
            End If
        End If
    End Sub
    Private Sub 採購清單_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 採購清單_dgv.RowPostPaint
        If 首次開啟 = True Then : Else
            For I As Integer = 0 To 採購清單_dgv.Rows.Count - 1
                If 採購清單_dgv("選擇", I).Value = True Then
                    採購清單_dgv.Rows(I).DefaultCellStyle.BackColor = Color.LightGreen
                Else
                    採購清單_dgv.Rows(I).DefaultCellStyle.BackColor = Color.White
                End If
            Next
        End If

        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 採購清單_dgv.Rows.Count - 1 Then : Exit Sub : Else
            If 採購清單_dgv("供應商", e.RowIndex).Value.ToString <> 採購清單_dgv("供應商", e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(採購清單_dgv.RowHeadersVisible, 採購清單_dgv.RowHeadersWidth, 0) : Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 採購清單_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 採購清單_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY) : Exit Sub
            End If
        End If
    End Sub
    Private Sub 採購清單_dgv_CellEndEdit(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 採購清單_dgv.CellEndEdit
        Set_合記計算()
        PA20 = 採購清單_dgv(8, e.RowIndex).Value.ToString : SQL_銷售明細_改倉名()
        If dr.Read() Then : 採購清單_dgv("倉儲狀態", e.RowIndex).Value = dr("倉名編號") : End If
        PA19 = 採購清單_dgv(11, e.RowIndex).Value.ToString : SQL_銷售明細_改物料訂製狀態()
        If dr.Read() Then : 採購清單_dgv("訂製狀態", e.RowIndex).Value = dr("編碼") : End If
        PA18 = 採購清單_dgv(14, e.RowIndex).Value.ToString : SQL_銷售明細_倉儲狀態()
        If dr.Read() Then : 採購清單_dgv("物料狀態", e.RowIndex).Value = dr("編碼") : End If : conn.Close()

        Dim EU(3) As String
        EU(0) = 採購清單_dgv("採購日期", e.RowIndex).Value.ToString
        If EU(0) = "" Then : Else
            If IsDate(EU(0)) Then
                日期1_dtp.Text = EU(0) : 採購清單_dgv.Rows(e.RowIndex).Cells("採購日期").Value = Strings.Format(日期1_dtp.Value, "yyyy/MM/dd")
            Else
                採購清單_dgv.Rows(e.RowIndex).Cells("採購日期").Value = ""
            End If
        End If
        EU(1) = 採購清單_dgv("預計進廠日", e.RowIndex).Value.ToString
        If EU(1) = "" Then : Else
            If IsDate(EU(1)) Then
                日期2_dtp.Text = EU(1) : 採購清單_dgv.Rows(e.RowIndex).Cells("預計進廠日").Value = Strings.Format(日期2_dtp.Value, "yyyy/MM/dd")
            Else
                採購清單_dgv.Rows(e.RowIndex).Cells("預計進廠日").Value = ""
            End If
        End If
        If 採購清單_dgv.Rows(e.RowIndex).Cells("已入庫").Value = False Then
            採購清單_dgv.Rows(e.RowIndex).Cells("實際進廠日").Value = ""
        Else
            EU(2) = 採購清單_dgv("實際進廠日", e.RowIndex).Value.ToString
            If EU(2) = "" Then : Else
                If IsDate(EU(2)) Then
                    日期3_dtp.Text = EU(2) : 採購清單_dgv.Rows(e.RowIndex).Cells("實際進廠日").Value = Strings.Format(日期3_dtp.Value, "yyyy/MM/dd")
                Else
                    採購清單_dgv.Rows(e.RowIndex).Cells("實際進廠日").Value = ""
                End If
            End If
        End If
        EU(3) = 採購清單_dgv("過帳日期", e.RowIndex).Value.ToString
        If EU(3) = "" Then : Else
            If IsDate(EU(3)) Then
                日期4_dtp.Text = EU(3) : 採購清單_dgv.Rows(e.RowIndex).Cells("過帳日期").Value = Strings.Format(日期4_dtp.Value, "yyyy/MM/dd")
            Else
                採購清單_dgv.Rows(e.RowIndex).Cells("過帳日期").Value = "" : 採購清單_dgv("選擇", e.RowIndex).Value = False
            End If
        End If
    End Sub
    Private Sub 採購清單_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 採購清單_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            項次_tb.Text = 採購清單_dgv(1, e.RowIndex).Value.ToString : 申請人_tb.Text = 採購清單_dgv("申請人", e.RowIndex).Value.ToString
            版次_tb.Text = 採購清單_dgv("訂製圖號", e.RowIndex).Value.ToString : 單號1_tb.Text = 採購清單_dgv("銷售單號", e.RowIndex).Value.ToString
            料號_tb.Text = 採購清單_dgv("料號", e.RowIndex).Value.ToString : 供應商_tb.Text = 採購清單_dgv("供應商", e.RowIndex).Value.ToString
            供應商_cb.Text = 採購清單_dgv("供應商", e.RowIndex).Value.ToString
            Set_合記計算()
            PA15 = 採購清單_dgv(0, e.RowIndex).Value.ToString : Set_清單5()

            物料圖_pb.Image = Nothing : PA1 = 採購清單_dgv.Rows(e.RowIndex).Cells("料號").Value : SQL_物料檔案_是否有圖()
            If dr.Read() Then
                If dr("是否有圖") = True Then
                    物料圖_pb.BackgroundImage = Nothing
                    PA = 採購清單_dgv.Rows(e.RowIndex).Cells("料號").Value : SQL_物料圖庫_圖片顯示() '--------------------圖片顯示
                    While dr.Read() = True
                        Dim unused As Byte() = New Byte(-1) {}
                        Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                        Dim oStream As New MemoryStream(bytes)
                        物料圖_pb.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                    物料圖_pb.SizeMode = 4
                Else
                    物料圖_pb.BackgroundImage = My.Resources.底圖1 : 物料圖_pb.BackgroundImageLayout = 3
                    物料圖_pb.Image = Nothing
                End If
            End If : conn.Close()
            If 批量勾選_cb.Checked = True Then
                If 採購清單_dgv("選擇", e.RowIndex).Value = False Then
                    採購清單_dgv("選擇", e.RowIndex).Value = True
                    If 採購清單_dgv("實際進廠日", e.RowIndex).Value.ToString = "" Then
                        If 已確認_ch.Checked = False Then
                            PA = InputBox("請輸入指定過帳日期")
                        Else
                            PA = InputBox("請輸入指定過帳日期", "", Strings.Format(日期5_dtp.Value, "yyyy/MM/dd"))
                        End If
                        If IsDate(PA) Then
                            If 已確認_ch.Checked = False Then
                                採購清單_dgv("過帳日期", e.RowIndex).Value = PA
                            Else
                                採購清單_dgv("過帳日期", e.RowIndex).Value = Strings.Format(日期5_dtp.Value, "yyyy/MM/dd")
                            End If
                            Dim EU As String
                            EU = 採購清單_dgv("過帳日期", e.RowIndex).Value.ToString
                            If EU = "" Then : Else
                                日期4_dtp.Text = EU : 採購清單_dgv.Rows(e.RowIndex).Cells("過帳日期").Value = Strings.Format(日期4_dtp.Value, "yyyy/MM/dd")
                            End If
                        Else
                            MsgBox("請輸入日期格式資料!!!!")
                            採購清單_dgv("過帳日期", e.RowIndex).Value = ""
                            採購清單_dgv("選擇", e.RowIndex).Value = False
                        End If
                    Else
                        If 已確認_ch.Checked = False Then
                            採購清單_dgv("過帳日期", e.RowIndex).Value = 採購清單_dgv("預計進廠日", e.RowIndex).Value.ToString
                        Else
                            採購清單_dgv("過帳日期", e.RowIndex).Value = Strings.Format(日期5_dtp.Value, "yyyy/MM/dd")
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub 外埔報表_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 外埔報表_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            流水號_tb.Text = 外埔報表_dgv("流水號", e.RowIndex).Value.ToString : 供應商編碼_cb.Text = 外埔報表_dgv("廠商編碼", e.RowIndex).Value.ToString
            內容_tb.Text = 外埔報表_dgv("備註內容", e.RowIndex).Value.ToString : 金額_nud.Text = 外埔報表_dgv("金額", e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub 採購清單_dgv_CellDoubleClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 採購清單_dgv.CellDoubleClick
        If e.RowIndex = -1 Then : Else
            採購清單_dgv("選擇", e.RowIndex).Value = False
            採購清單_dgv("過帳日期", e.RowIndex).Value = ""
        End If
    End Sub
    Private Sub 訂製圖檔_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 訂製圖檔_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            版次_tb.Text = 訂製圖檔_dgv.Rows(e.RowIndex).Cells("版次").Value
            '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            物料圖1_pb.Image = Nothing : PA15 = 項次_tb.Text : PA16 = 版次_tb.Text : SQL_採購控制表_訂製圖片()
            While dr.Read() = True
                Dim unused As Byte() = New Byte(-1) {}
                Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                Dim oStream As New MemoryStream(bytes)
                物料圖1_pb.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close()
            物料圖1_pb.SizeMode = 4
        End If
    End Sub
    Private Sub 地區1_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 地區1_cb.SelectedIndexChanged
        If 地區1_cb.Text = "" And 查詢1_tb.Text = "" Then
            供應商1_cb.Items.Clear() : 供應商編碼1_cb.Items.Clear() : Set_清單1()
        ElseIf 地區1_cb.Text = "" Then
            查詢1_tb.Text = "" : 供應商1_cb.Items.Clear() : 供應商編碼1_cb.Items.Clear()
        ElseIf 地區1_cb.Text <> "" Then
            查詢1_tb.Text = "" : 供應商1_下拉表單資料載入()
        End If
    End Sub
    Private Sub 供應商1_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商1_cb.SelectedIndexChanged
        If 地區1_cb.Text <> "" Then
            查詢1_tb.Text = "" : 供應商編碼1_cb.SelectedIndex = 供應商1_cb.SelectedIndex
        End If
    End Sub
    Private Sub 供應商編碼1_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商編碼1_cb.SelectedIndexChanged
        If 地區1_cb.Text <> "" Then
            查詢1_tb.Text = "" : 供應商1_cb.SelectedIndex = 供應商編碼1_cb.SelectedIndex : 供應商編碼1_tb.Text = 供應商編碼1_cb.Text : Set_清單1()
        End If
    End Sub
    Private Sub 供應商_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商_cb.SelectedIndexChanged
        供應商編碼_cb.SelectedIndex = 供應商_cb.SelectedIndex
    End Sub
    Private Sub 供應商編碼_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商編碼_cb.SelectedIndexChanged
        供應商_cb.SelectedIndex = 供應商編碼_cb.SelectedIndex
    End Sub
    Private Sub 業務1_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 業務1_cb.SelectedIndexChanged
        Set_清單1()
    End Sub
    Private Sub 查詢1_tb_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 查詢1_tb.KeyPress
        If e.KeyChar = Chr(13) Then
            供應商1_cb.Items.Clear() : 供應商編碼1_cb.Items.Clear()
            關鍵字 = True : PA = 查詢1_tb.Text : Set_清單1() : 地區1_cb.Text = ""
        End If
    End Sub
    Private Sub 清空1_bt_Click(sender As Object, e As EventArgs) Handles 清空1_bt.Click
        業務1_cb.Text = "" : 查詢1_tb.Text = "" : 地區1_cb.Text = ""
    End Sub
    Private Sub 窄間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 小間距_ch.Click
        小間距_ch.Checked = True : 中間距_ch.Checked = False : 大間距_ch.Checked = False
        Dim aa1 As MsgBoxResult = MsgBox("是否需要存檔?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            存檔1() : Set_清單1()
        Else
            Set_清單1()
        End If
    End Sub
    Private Sub 中間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 中間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = True : 大間距_ch.Checked = False
        Dim aa1 As MsgBoxResult = MsgBox("是否需要存檔?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            存檔1() : Set_清單1()
        Else
            Set_清單1()
        End If
    End Sub
    Private Sub 大間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 大間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = False : 大間距_ch.Checked = True
        Dim aa1 As MsgBoxResult = MsgBox("是否需要存檔?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            存檔1() : Set_清單1()
        Else
            Set_清單1()
        End If
    End Sub
    Private Sub 檔案名稱_tb_DragEnter(sender As Object, e As DragEventArgs) Handles 檔案名稱_tb.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            files = CType(e.Data.GetData(DataFormats.FileDrop), String())
            檔案名稱_tb.Text = files(files.Length - 1)
        End If
        Dim STR2 As Integer = 0
        檔名讀取_tb.Text = 檔案名稱_tb.Text
        If 檔案名稱_tb.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(檔名讀取_tb.Text)
                If Strings.Right((檔名讀取_tb.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        檔名讀取_tb.Text = ""
                        檔案名稱_tb.Text = ""
                        i = 9999
                    Else
                        檔名讀取_tb.Text = Strings.Left((檔名讀取_tb.Text), STR1)
                    End If
                Else
                    檔名讀取_tb.Text = Strings.Right((檔案名稱_tb.Text), STR2)
                    檔案名稱_tb.Text = 檔名讀取_tb.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub 檔案名稱_tb_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles 檔案名稱_tb.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            檔案名稱_tb.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub 刪除單據上點選的資料_bt_Click(sender As Object, e As EventArgs) Handles 刪除單據上點選的資料_bt.Click
        If gUserName <> 申請人_tb.Text And Val(登入人級別) >= 3 Then
            MsgBox("不能修改其他申請人的資料")
        Else
            PA = 項次_tb.Text : SQL_採購控制表_刪除()
        End If
        Set_清單1() : MsgBox("已刪除選中的資料")
    End Sub
    Private Sub 讀取資料夾路徑_bt_Click(sender As Object, e As EventArgs) Handles 讀取資料夾路徑_bt.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then : 本機資料夾_wb.Navigate(FolderBrowserDialog1.SelectedPath) : End If
    End Sub
    Private Sub 圖檔入系統_bt_Click(sender As Object, e As EventArgs) Handles 圖檔入系統_bt.Click
        If 項次_tb.Text = "" Then : MsgBox("請先選擇要指定訂製圖檔的採購資料!!") : Else
            If 檔案名稱_tb.Text <> "" Then
                Dim 驗證 As String : Dim 取變數 As String
                取變數 = Strings.StrReverse(檔案名稱_tb.Text)
                For i As Integer = 1 To 10
                    If i = 1 Then
                        驗證 = Strings.Left(取變數, i)
                    Else
                        If Strings.Mid(取變數, i, 1) = "." Then
                            驗證 = Strings.StrReverse(驗證) : Exit For
                        Else
                            驗證 = 驗證 & Strings.Mid(取變數, i, 1)
                        End If
                    End If
                Next

                If 驗證 = "PNG" Or 驗證 = "png" Or 驗證 = "GIF" Or 驗證 = "gif" Or 驗證 = "BMP" Or 驗證 = "bmp" Or 驗證 = "JPG" Or 驗證 = "jpg" Or 驗證 = "JPEG" Or 驗證 = "jpeg" Then
                    Dim 進版 As Integer
                    If 版次_tb.Text = "" Then
                        版次_tb.Text = "V01"
                    Else
                        進版 = Strings.Right(版次_tb.Text, 2) : 進版 += 1
                        If 進版 < 10 Then : 版次_tb.Text = "V" & "0" & 進版
                        Else : 版次_tb.Text = "V" & EDR : End If
                    End If

                    PA1 = 項次_tb.Text : PA2 = 單號1_tb.Text : PA3 = 版次_tb.Text : PA4 = 檔案名稱_tb.Text
                    SQL_採購控制表_圖片新增() : SQL_採購控制表_修改() : conn.Close()
                    '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                    frq = CType(WebRequest.Create(New Uri(Target1 & 檔案名稱_tb.Text)), FtpWebRequest)
                    fcr = New NetworkCredential(FTP帳號, FTP密碼)
                    frq.Credentials = fcr
                    frq.Method = WebRequestMethods.Ftp.DeleteFile
                    frq.UseBinary = True
                    frp = CType(frq.GetResponse, FtpWebResponse)
                    frp.Close()
                    FTP資料夾_wb.Refresh()
                    '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    物料圖1_pb.Image = Nothing : PA15 = 項次_tb.Text : PA16 = 版次_tb.Text : SQL_採購控制表_訂製圖片()
                    While dr.Read() = True
                        Dim unused As Byte() = New Byte(-1) {}
                        Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                        Dim oStream As New MemoryStream(bytes)
                        物料圖1_pb.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                    物料圖1_pb.SizeMode = 4
                    '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    存檔1() : Set_清單5() : Set_清單1() : 檔案名稱_tb.Text = ""
                Else
                    MsgBox("上傳文件並非常用圖片格式，文件已自動刪除，請專換成常用圖片檔案再行操作。")
                    '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                    frq = CType(WebRequest.Create(New Uri(Target1 & 檔案名稱_tb.Text)), FtpWebRequest)
                    fcr = New NetworkCredential(FTP帳號, FTP密碼)
                    frq.Credentials = fcr
                    frq.Method = WebRequestMethods.Ftp.DeleteFile
                    frq.UseBinary = True
                    frp = CType(frq.GetResponse, FtpWebResponse)
                    frp.Close()
                    FTP資料夾_wb.Refresh()
                    檔案名稱_tb.Text = ""
                End If
            End If
        End If
    End Sub
    Private Sub 下載圖檔_bt_Click(sender As Object, e As EventArgs) Handles 下載圖檔_bt.Click
        If 物料圖1_pb.Image Is Nothing Then : MsgBox("所選擇的資料沒有圖片檔案，無法下載。") : Else
            With SaveFileDialog1 : .Filter = "所有文件(*.jpg)|*.jpg" : End With
            SaveFileDialog1.FileName = 單號1_tb.Text & "-" & 料號_tb.Text & "-" & 版次_tb.Text & ".jpg"

            Dim result As DialogResult = SaveFileDialog1.ShowDialog()
            下載檔名_tb.Text = SaveFileDialog1.FileName
            If result = System.Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If

            PA15 = 項次_tb.Text : PA16 = 版次_tb.Text
            ds3.Clear() : SQL_採購控制表_訂製圖片下載() : da.Fill(ds3)
            If ds3.Tables(0).Rows.Count > 0 Then
                Dim imgData() As Byte
                imgData = ds3.Tables(0).Rows(0).Item("圖片")
                Dim fs As FileStream
                fs = File.Create(下載檔名_tb.Text, imgData.Length - 1)
                fs.Write(imgData, 0, imgData.Length - 1)
                fs.Close()
            End If : conn.Close() : MsgBox("下載完成")
        End If
    End Sub
    Private Sub 大圖預覽_bt_Click(sender As Object, e As EventArgs) Handles 大圖預覽_bt.Click
        If 項次_tb.Text = "" Or 版次_tb.Text = "" Then
            MsgBox("沒有訂製圖或是沒有決哲訂製圖")
        Else
            採購訂製大圖.ShowDialog()
        End If
    End Sub
    Private Sub 存檔1()
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            資料數 = 採購清單_dgv.Rows.Count : MyModule1.進度條()
            If gUserName <> 採購清單_dgv("申請人", i).Value And Val(登入人級別) >= 3 Then
            Else
                PA = 採購清單_dgv("資料流水號", i).Value : PA1 = 採購清單_dgv("物料狀態", i).Value : PA2 = 採購清單_dgv("訂製狀態", i).Value : PA3 = 採購清單_dgv("倉儲狀態", i).Value
                PA4 = 採購清單_dgv("備註", i).Value.ToString : PA5 = 採購清單_dgv("採購日期", i).Value.ToString : PA6 = 採購清單_dgv("預計進廠日", i).Value.ToString
                NU1 = 採購清單_dgv("數量", i).Value : NU2 = 採購清單_dgv("成本", i).Value : NU3 = NU1 * NU2 : PA7 = 採購清單_dgv("實際進廠日", i).Value.ToString
                PA8 = 採購清單_dgv("銷售流水", i).Value.ToString
                SQL_採購控制表_存檔()
            End If
        Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 : Set_清單1()
    End Sub
    Private Sub 存檔2()
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            資料數 = 採購清單_dgv.Rows.Count : MyModule1.進度條()
            If gUserName <> 採購清單_dgv("申請人", i).Value And Val(登入人級別) >= 3 Then
            Else
                PA = 採購清單_dgv("資料流水號", i).Value : PA1 = 採購清單_dgv("物料狀態", i).Value : PA2 = 採購清單_dgv("訂製狀態", i).Value : PA3 = 採購清單_dgv("倉儲狀態", i).Value
                PA4 = 採購清單_dgv("備註", i).Value.ToString : PA5 = 採購清單_dgv("採購日期", i).Value.ToString : PA6 = 採購清單_dgv("預計進廠日", i).Value.ToString
                NU1 = 採購清單_dgv("數量", i).Value : NU2 = 採購清單_dgv("成本", i).Value : NU3 = NU1 * NU2 : PA35 = 採購清單_dgv("實際進廠日", i).Value.ToString
                PA7 = 採購清單_dgv("預計進廠日", i).Value.ToString : PA8 = 採購清單_dgv("銷售流水", i).Value.ToString
                SQL_採購控制表_存檔2()
            End If
        Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 : Set_清單1()
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        Dim coo As Boolean = True
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            If 採購清單_dgv("實際進廠日", i).Value.ToString = "" Then : coo = False : End If : Exit For
        Next
        If coo = True Then
            存檔1()
        Else
            存檔2()
        End If
        MsgBox("修改完成。")
    End Sub
    Private Sub 過帳_bt_Click(sender As Object, e As EventArgs) Handles 過帳_bt.Click
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            資料數 = 採購清單_dgv.Rows.Count : MyModule1.進度條()
            If 採購清單_dgv("選擇", i).Value = True Then
                If 採購清單_dgv("過帳日期", i).Value.ToString <> "" Then
                    PA = 採購清單_dgv("資料流水號", i).Value : PA2 = "1" : PA1 = 採購清單_dgv("過帳日期", i).Value
                    SQL_採購控制表_過帳()
                End If
            End If
        Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 : Set_清單1()
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        採購清單_dgv.RowsDefaultCellStyle.Font = New Font("微軟正黑體", 字體_NUD.Value)
        訂製圖檔_dgv.RowsDefaultCellStyle.Font = New Font("微軟正黑體", 字體_NUD.Value)
    End Sub
    Private Sub 全部選擇_bt_Click(sender As Object, e As EventArgs) Handles 全部選擇_bt.Click
        If 供應商_tb.Text = "" Then
            MsgBox("請先點選供應商!")
        Else
            For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
                If 供應商_tb.Text = 採購清單_dgv.Rows(i).Cells("供應商").Value Then
                    If 採購清單_dgv.Rows(i).Cells("實際進廠日").Value.ToString <> "" Then
                        採購清單_dgv.Rows(i).Cells("選擇").Value = True
                        If 已確認_ch.Checked = False Then
                            採購清單_dgv("過帳日期", i).Value = 採購清單_dgv("預計進廠日", i).Value.ToString
                        Else
                            採購清單_dgv("過帳日期", i).Value = Strings.Format(日期5_dtp.Value, "yyyy/MM/dd")
                        End If
                    End If
                End If
            Next
        End If
    End Sub
    Private Sub 全部取消_bt_Click(sender As Object, e As EventArgs) Handles 全部取消_bt.Click
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            If 供應商_tb.Text = 採購清單_dgv.Rows(i).Cells("供應商").Value Then
                採購清單_dgv.Rows(i).Cells("選擇").Value = False : 採購清單_dgv("過帳日期", i).Value = ""
            End If
        Next
    End Sub
    Private Sub 外埔新增_bt_Click(sender As Object, e As EventArgs) Handles 外埔新增_bt.Click
        If 供應商編碼_cb.Text = "" Then
            MsgBox("請先選擇上方單據，再新增備註內容!!")
        Else
            SQL_採購外埔表_最後一筆資料()
            If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("流水號").ToString, 9)) : Else : EDR = 0 : End If
            conn.Close() : EDR += 1
            If EDR < 10 Then : ESTR = "ES" & "00000000" & EDR
            ElseIf EDR > 9 And EDR < 100 Then : ESTR = "ES" & "0000000" & EDR
            ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "ES" & "000000" & EDR
            ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "ES" & "00000" & EDR
            ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "ES" & "0000" & EDR
            ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "ES" & "000" & EDR
            ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "ES" & "00" & EDR
            ElseIf EDR > 9999999 And EDR < 10000000 Then : ESTR = "ES" & "0" & EDR
            ElseIf EDR > 99999999 Then : ESTR = "ES" & EDR : End If

            PA31 = ESTR : PA32 = "" : PA33 = 金額_nud.Value : PA34 = 內容_tb.Text : PA35 = 供應商編碼_cb.Text : PA36 = 單號1_tb.Text
            SQL_採購外埔表_新增() : Set_清單4() : MsgBox("新增完成!!")
        End If
    End Sub
    Private Sub 外埔修改_bt_Click(sender As Object, e As EventArgs) Handles 外埔修改_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇單據!!") : Else
            PA33 = 金額_nud.Value : PA34 = 內容_tb.Text : PA35 = 流水號_tb.Text : SQL_採購外埔表_修改() : Set_清單4() : MsgBox("修改完成!!")
        End If
    End Sub
    Private Sub 外埔刪除_bt_Click(sender As Object, e As EventArgs) Handles 外埔刪除_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇單據!!") : Else : PA35 = 流水號_tb.Text : SQL_採購外埔表_刪除() : Set_清單4() : MsgBox("刪除完成!!") : End If
    End Sub
    Private Sub 外埔過帳_bt_Click(sender As Object, e As EventArgs) Handles 外埔過帳_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇單據!!") : Else
            PA32 = Strings.Format(日期5_dtp.Value, "yyyy/MM/dd") : PA35 = 流水號_tb.Text : SQL_採購外埔表_過帳() : Set_清單4() : MsgBox("修改完成!!")
        End If
    End Sub
    Private Sub 重新整理_tsm_Click(sender As Object, e As EventArgs) Handles 重新整理_tsm.Click
        Set_清單1()
    End Sub
    Private Sub 未過帳_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 未過帳_ch.Click
        If 未過帳_ch.Checked = True Then
            未過帳_ch.Checked = True : 已過帳_ch.Checked = False
        Else
            未過帳_ch.Checked = False : 已過帳_ch.Checked = True
        End If : Set_清單1()
    End Sub
    Private Sub 已過帳_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 已過帳_ch.Click
        If 已過帳_ch.Checked = True Then
            未過帳_ch.Checked = False : 已過帳_ch.Checked = True
        Else
            未過帳_ch.Checked = True : 已過帳_ch.Checked = False
        End If : Set_清單1()
    End Sub
    Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
        Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
    End Function
    Private Sub 列印Excel_bt_Click(sender As Object, e As EventArgs) Handles 列印Excel_bt.Click
        xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
        xlApp.Visible = True
        xlApp.DisplayAlerts = True
        xlApp.Application.WindowState = xlMaximized
        xlBook = xlApp.Workbooks.Add
        xlSheet = NewMethod(xlBook)
        xlBook.Activate()
        xlSheet.Activate()
        xlSheet.Cells(1, 1) = "雅邸家具有限公司-採購對帳單 "
        xlSheet.Cells(2, 1) = "銷售單號"
        xlSheet.Cells(2, 2) = "供應商"
        xlSheet.Cells(2, 3) = "料號"
        xlSheet.Cells(2, 4) = "品名"
        xlSheet.Cells(2, 5) = "倉名"
        xlSheet.Cells(2, 6) = "訂製"
        xlSheet.Cells(2, 7) = "狀態"
        xlSheet.Cells(2, 8) = "備註"
        xlSheet.Cells(2, 9) = "數量"
        xlSheet.Cells(2, 10) = "實際進廠日"

        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            xlSheet.Cells(i + 3, 1) = 採購清單_dgv.Rows(i).Cells("銷售單號").Value
            xlSheet.Cells(i + 3, 2) = 採購清單_dgv.Rows(i).Cells("供應商").Value
            xlSheet.Cells(i + 3, 3) = 採購清單_dgv.Rows(i).Cells("料號").Value
            xlSheet.Cells(i + 3, 4) = 採購清單_dgv.Rows(i).Cells("品名").Value
            xlSheet.Cells(i + 3, 5) = 採購清單_dgv.Rows(i).Cells("倉名").Value
            xlSheet.Cells(i + 3, 6) = 採購清單_dgv.Rows(i).Cells("訂製").Value
            xlSheet.Cells(i + 3, 7) = 採購清單_dgv.Rows(i).Cells("狀態").Value
            xlSheet.Cells(i + 3, 8) = 採購清單_dgv.Rows(i).Cells("備註").Value
            xlSheet.Cells(i + 3, 9) = 採購清單_dgv.Rows(i).Cells("數量").Value
            xlSheet.Cells(i + 3, 10) = 採購清單_dgv.Rows(i).Cells("實際進廠日").Value
            N2 = i + 3
        Next i

        BB(xlApp, xlSheet)
        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlApp.Application.WindowState = xlMinimized
        MsgBox("列印完成!!!")
    End Sub
    Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
        xlSheet.Range("A1:J9").Select()
        xlSheet.Range("H9").Activate()

        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 11 : .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 : .Size = 22 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False : .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : End With

        xlSheet.Rows("2:2").Select
        With myExcel.Selection.Font : .Size = 12 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False : .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : End With
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("A3:J9").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.Columns("A:A").Select : xlSheet.Range("A2").Activate() : myExcel.Selection.ColumnWidth = 9
        xlSheet.Columns("A:A").Select : xlSheet.Range("A2").Activate()
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : End With
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : End With

        xlSheet.Columns("B:B").Select : xlSheet.Range("B2").Activate() : myExcel.Selection.EntireColumn.Hidden = True
        xlSheet.Columns("B:B").Select : xlSheet.Range("B2").Activate() : myExcel.Selection.ColumnWidth = 8
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : End With
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : End With

        xlSheet.Columns("C:C").Select : xlSheet.Range("C2").Activate() : myExcel.Selection.ColumnWidth = 10
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : End With
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : End With

        xlSheet.Columns("D:D").Select : xlSheet.Range("D2").Activate() : myExcel.Selection.ColumnWidth = 30
        xlSheet.Columns("E:E").Select : xlSheet.Range("E2").Activate() : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("F:F").Select : myExcel.Selection.ColumnWidth = 9
        xlSheet.Columns("G:G").Select : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("H:H").Select : myExcel.Selection.ColumnWidth = 30

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

        xlSheet.Columns("J:J").Select : myExcel.Selection.ColumnWidth = 12
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("A1:J2").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

        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : 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.Range("A3:J" & N2).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

        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : 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

        myExcel.ActiveWindow.DisplayGridlines = False : xlSheet.Rows("3:3").Select
        myExcel.ActiveWindow.FreezePanes = True : xlSheet.Rows("1:1").Select
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.UnMerge
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
        With myExcel.Selection
            .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        myExcel.Selection.UnMerge : xlSheet.Range("A1:J1").Select()
        With myExcel.Selection
            .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        myExcel.Selection.Merge : xlSheet.Range("K1:O1").Select()
        myExcel.Selection.ClearContents : myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeLeft).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeTop).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeBottom).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone
        myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("A1:J1").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.Range("A1:J2").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

        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Rows("1:1").Select
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeLeft).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeTop).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeBottom).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone
        myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("A1:J2").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeLeft).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeTop).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeBottom).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone
        myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
        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

        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : 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
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : End With
    End Sub
End Class