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
Imports Microsoft.Office.Interop.Excel
Public Class 物料檔案
    ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet
    Dim 資料新增判斷 As Boolean : Dim EDR, N1 As Integer : Dim ESTR As String : Dim 首次開啟 As Boolean = False
    Dim xlApp As Application : Dim xlBook As Workbook : Dim xlSheet As Worksheet
    Private Sub Set_清單()
        物料清單_dgv.DataSource = Nothing : ds1.Clear() : 物料清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        物料清單_dgv.ColumnHeadersHeight = 40
        物料清單_dgv.AllowUserToAddRows = False
        物料清單_dgv.RowTemplate.Height = 30

        SQL_物料檔案_清單()
        da.Fill(ds1) : 物料清單_dgv.DataSource = ds1.Tables(0) : conn.Close()

        物料清單_dgv.Columns("料號").FillWeight = 70 : 物料清單_dgv.Columns("供應商").FillWeight = 40 : 物料清單_dgv.Columns("成本").FillWeight = 50
        物料清單_dgv.Columns("品名").FillWeight = 170 : 物料清單_dgv.Columns("停用").FillWeight = 40 : 物料清單_dgv.Columns("圖片").FillWeight = 40
        物料清單_dgv.Columns("刪除").FillWeight = 40 : 物料清單_dgv.Columns("最低庫存量").FillWeight = 40 : 物料清單_dgv.Columns("廠商").Visible = False

        物料清單_dgv.Columns("成本").DefaultCellStyle.Format = "#,##0"
        物料清單_dgv.Columns("成本").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        物料清單_dgv.Columns("成本").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        物料清單_dgv.Columns("料號").ReadOnly = True : 物料清單_dgv.Columns("供應商").ReadOnly = True : 物料清單_dgv.Columns("成本").ReadOnly = True
        物料清單_dgv.Columns("圖片").ReadOnly = True
        資料筆數_tb.Text = 物料清單_dgv.Rows.Count

        For i As Integer = 0 To 物料清單_dgv.Rows.Count - 1
            物料清單_dgv("刪除", i).Value = False
        Next
    End Sub
    Private Sub 供應商_下拉表單資料載入()
        BL1 = False : PA3 = "" : PA4 = ""
        If 地區_cb.Text = "" Then : PA3 = ""
        ElseIf 地區_cb.Text = "北部" Then : PA3 = "2"
        ElseIf 地區_cb.Text = "南部" Then : PA3 = "3"
        ElseIf 地區_cb.Text = "材料" Then : PA3 = "9"
        ElseIf 地區_cb.Text = "調貨" Then : PA3 = "Y"
        ElseIf 地區_cb.Text = "其他" Then : PA3 = "1"
        End If
        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
        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)

        地區編碼_cb.Enabled = False : 下載檔名_tb.Visible = False : 檔名讀取_tb.Visible = False : 供應商編碼_tb.Visible = False '--------------------控件隱藏、操作許可設定與其他基本顯示設定
        料號_tb.Enabled = False : 批量存檔_bt.Enabled = False : 批輛停用選擇_ch.Enabled = False : 批量刪除選擇_ch.Enabled = False : 批量刪除_bt.Enabled = False : 狀態_lb.Text = "預覽狀態"

        If Val(登入人級別) <= 2 Then '--------------------刪除資料權限
            刪除_tsm.Enabled = True : 刪除_bt.Enabled = True : 批量刪除_bt.Enabled = True
        Else
            刪除_tsm.Enabled = False : 刪除_bt.Enabled = False : 批量刪除_bt.Enabled = False
        End If
        首次開啟 = False : Set_清單() : 首次開啟 = True '--------------------資料讀取

        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 Sub
    Private Sub 物料檔案_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        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 = 8
        End If
    End Sub
    Private Sub 供應商資料表_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        If 資料新增判斷 = True Then '--------------------如果再關閉介面時，資料新增判斷是開啟的，就無條件刪除控件上的那筆資料
            PA20 = 料號_tb.Text : SQL_物料檔案_刪除() '--------------------資料刪除
        End If
    End Sub
    Private Sub 物料清單_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 物料清單_dgv.CellClick
        If 首次開啟 = True Then
            If e.RowIndex = -1 Then : Else
                料號_tb.Text = 物料清單_dgv("料號", e.RowIndex).Value.ToString : 供應商編碼_tb.Text = 物料清單_dgv("供應商", e.RowIndex).Value.ToString
                成本_nud.Value = 物料清單_dgv("成本", e.RowIndex).Value : 品名_tb.Text = 物料清單_dgv("品名", e.RowIndex).Value.ToString
                停用_ch.Checked = 物料清單_dgv("停用", e.RowIndex).Value

                物料圖_pb.Image = Nothing : PA1 = 料號_tb.Text : SQL_物料檔案_是否有圖()
                If dr.Read() Then
                    If dr("是否有圖") = True Then
                        物料圖_pb.BackgroundImage = Nothing
                        PA = 料號_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)
                            物料圖_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 批輛停用選擇_ch.Checked = True Then '--------------------批輛停用選擇功能
                    If 物料清單_dgv("停用", e.RowIndex).Value = False Then
                        物料清單_dgv("停用", e.RowIndex).Value = True
                    ElseIf 物料清單_dgv("停用", e.RowIndex).Value = True Then
                        物料清單_dgv("停用", e.RowIndex).Value = False
                    End If
                End If

                If 批量刪除選擇_ch.Checked = True Then '--------------------批輛停用選擇功能
                    If 物料清單_dgv("刪除", e.RowIndex).Value = False Then
                        物料清單_dgv("刪除", e.RowIndex).Value = True
                    ElseIf 物料清單_dgv("刪除", e.RowIndex).Value = True Then
                        物料清單_dgv("刪除", e.RowIndex).Value = False
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub 物料清單_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 物料清單_dgv.RowPostPaint
        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
            For i As Integer = 0 To 物料清單_dgv.Rows.Count - 1
                If 物料清單_dgv("圖片", i).Value = True And 物料清單_dgv("停用", i).Value = False Then : 物料清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow : End If
                If 物料清單_dgv("圖片", i).Value = False And 物料清單_dgv("停用", i).Value = False Then : 物料清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.White : End If
                If 物料清單_dgv("停用", i).Value = True Then : 物料清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightPink : End If
                If 物料清單_dgv("刪除", i).Value = True Then : 物料清單_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Red : End If
                If 物料清單_dgv("刪除", i).Value = False Then : 物料清單_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black : End If
            Next
        End If
    End Sub
    Private Sub 地區_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 地區_cb.SelectedIndexChanged
        If 地區_cb.Text = "" And 查詢_tb.Text = "" Then
            供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear() : Set_清單()
        ElseIf 地區_cb.Text = "" Then
            查詢_tb.Text = "" : 供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear()
        ElseIf 地區_cb.Text <> "" Then
            查詢_tb.Text = "" : 供應商_下拉表單資料載入()
        End If
    End Sub
    Private Sub 供應商_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商_cb.SelectedIndexChanged
        If 地區_cb.Text <> "" Then
            查詢_tb.Text = "" : 供應商編碼_cb.SelectedIndex = 供應商_cb.SelectedIndex
        End If
    End Sub
    Private Sub 供應商編碼_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商編碼_cb.SelectedIndexChanged
        If 地區_cb.Text <> "" Then
            查詢_tb.Text = "" : 供應商_cb.SelectedIndex = 供應商編碼_cb.SelectedIndex : 供應商編碼_tb.Text = 供應商編碼_cb.Text : Set_清單()
        End If
    End Sub
    Private Sub 清空_bt_Click(sender As Object, e As EventArgs) Handles 清空_bt.Click
        查詢_tb.Text = "" : 地區_cb.Text = ""
    End Sub
    Private Sub 查詢_tb_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 查詢_tb.KeyPress
        If e.KeyChar = Chr(13) Then
            供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear()
            PA = 查詢_tb.Text : Set_清單() : 地區_cb.Text = ""
        End If
    End Sub
    Private Sub 圖檔入系統_bt_Click(sender As Object, e As EventArgs) Handles 圖檔入系統_bt.Click
        If 料號_tb.Text = "" Then : MsgBox("請先選擇未指定圖片的物料料號!!") : Else
            開啟檔案.Filter = "圖片文件 (*.jpg; *.jpeg; *.png; *.bmp)|*.jpg;*.jpeg;*.png;*.bmp"
            開啟檔案.Title = "選擇圖片文件"
            開啟檔案.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
            If 開啟檔案.ShowDialog() = DialogResult.OK Then
                Try
                    物料圖_pb.SizeMode = 4
                    PA49 = 開啟檔案.FileName : SQL_物料檔案_圖片修改()
                    'Set_附件清單() : MsgBox("附件導入系統完成!!")
                    MsgBox("圖片導入系統完成!!")

                    物料圖_pb.Image = Nothing : PA1 = 料號_tb.Text : SQL_物料檔案_是否有圖()
                    If dr.Read() Then
                        If dr("是否有圖") = True Then
                            物料圖_pb.BackgroundImage = Nothing
                            PA = 料號_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)
                                物料圖_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()
                Catch ex As Exception
                    MessageBox.Show("無法加載圖片: " & ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End If
    End Sub
    Private Sub 下載圖檔_bt_Click(sender As Object, e As EventArgs) Handles 下載圖檔_bt.Click
        If 物料圖_pb.Image Is Nothing Then : MsgBox("所選擇的資料沒有圖片檔案，無法下載。") : Else
            With SaveFileDialog1 : .Filter = "所有文件(*.jpg)|*.jpg" : End With
            SaveFileDialog1.FileName = 料號_tb.Text & "-" & Strings.Format(Today(), "yyyyMMdd") & ".jpg"
            Dim result As DialogResult = SaveFileDialog1.ShowDialog()
            下載檔名_tb.Text = SaveFileDialog1.FileName
            If result = System.Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If

            conn.Close() : ds2.Clear() : SQL_物料圖庫_圖片下載() : da.Fill(ds2)
            If ds2.Tables(0).Rows.Count > 0 Then
                Dim imgData() As Byte
                imgData = ds2.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 供應商編碼_cb.Text = "" Then '--------------------設定條件讓業務控件不為空
            MsgBox("沒有選擇供應商")
        Else
            料號_tb.Text = "" : 供應商編碼_tb.Text = "" : 成本_nud.Value = 0 : 品名_tb.Text = "" : 停用_ch.Checked = False '--------------------控件清空
            查詢_tb.Text = ""
            資料新增判斷 = True '--------------------目前的編輯狀態
            狀態_lb.Text = "編輯狀態"
            物料清單_dgv.Enabled = False : 查詢_tb.Enabled = False '--------------------關閉DGV與關鍵字查詢控件的點選控制功能

            PA = InputBox("過渡時期，手工請輸入料號!")
            料號_tb.Text = PA

            SQL_物料檔案_新增() '--------------------人事資料表中寫入一筆資料
        End If
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        If 品名_tb.Text = "" Then '--------------------設定條件讓地區編碼_cb控件不為空
            MsgBox("物品一定要有名稱。")
        Else
            資料新增判斷 = False '--------------------目前的編輯狀態
            狀態_lb.Text = "預覽狀態"
            物料清單_dgv.Enabled = True : 查詢_tb.Enabled = True '--------------------開啟DGV與關鍵字查詢控件的點選控制功能
            NU1 = 成本_nud.Value : PA1 = 品名_tb.Text : BL1 = 停用_ch.Checked : PA2 = 料號_tb.Text : NU2 = 低量_nud.Value
            SQL_物料檔案_修改() '--------------------資料修改
            Set_清單() '--------------------資料修改後重新讀取清單
        End If
    End Sub
    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        If 料號_tb.Text = "" Then '--------------------設定條件讓流水號控件不為空
            MsgBox("沒有選擇要刪除的資料")
        Else
            Dim aa1 As MsgBoxResult = MsgBox("確定要執行刪除指令嗎?", MsgBoxStyle.OkCancel)
            If aa1 = MsgBoxResult.Ok Then
                PA20 = 料號_tb.Text : SQL_物料檔案_刪除() '--------------------資料刪除
                Set_清單() '--------------------資料修改後重新讀取清單
            End If
        End If
    End Sub
    Private Sub 啟用批改_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 啟用批改_ch.Click
        If 啟用批改_ch.Checked = True Then
            批量存檔_bt.Enabled = True : 新增準備_bt.Enabled = False : 存檔_bt.Enabled = False : 刪除_bt.Enabled = False
            批輛停用選擇_ch.Enabled = True : 啟用批改_ch.Checked = True : 重新整理_tsm.Enabled = False
            新增準備_tsm.Enabled = False : 存檔_tsm.Enabled = False : 刪除_tsm.Enabled = False : 批量刪除選擇_ch.Enabled = True : 批量刪除_bt.Enabled = True
            物料清單_dgv.EditMode = DataGridViewEditMode.EditOnEnter : 狀態_lb.Text = "批改狀態" '--------------------控件隱藏、操作許可設定與其他基本顯示設定
            MsgBox("已啟用批改模式。")
        ElseIf 啟用批改_ch.Checked = False Then
            批量存檔_bt.Enabled = False : 新增準備_bt.Enabled = True : 存檔_bt.Enabled = True : 刪除_bt.Enabled = True
            批輛停用選擇_ch.Enabled = False : 批輛停用選擇_ch.Checked = False : 啟用批改_ch.Checked = False : 批量刪除選擇_ch.Checked = False : 重新整理_tsm.Enabled = True
            新增準備_tsm.Enabled = True : 存檔_tsm.Enabled = True : 刪除_tsm.Enabled = True : 批量刪除選擇_ch.Enabled = False : 批量刪除_bt.Enabled = False
            物料清單_dgv.EditMode = DataGridViewEditMode.EditOnF2 : 狀態_lb.Text = "預覽狀態" '--------------------控件隱藏、操作許可設定與其他基本顯示設定
        End If
    End Sub
    Private Sub 批量存檔_bt_Click(sender As Object, e As EventArgs) Handles 批量存檔_bt.Click
        批量存檔_bt.Enabled = False : 新增準備_bt.Enabled = True : 存檔_bt.Enabled = True : 刪除_bt.Enabled = True
        批輛停用選擇_ch.Enabled = False : 批輛停用選擇_ch.Checked = False : 啟用批改_ch.Checked = False : 重新整理_tsm.Enabled = True
        新增準備_tsm.Enabled = True : 存檔_tsm.Enabled = True : 刪除_tsm.Enabled = True : 批量刪除選擇_ch.Enabled = False : 批量刪除_bt.Enabled = False
        物料清單_dgv.EditMode = DataGridViewEditMode.EditOnF2 : 狀態_lb.Text = "預覽狀態" '--------------------控件隱藏、操作許可設定與其他基本顯示設定

        For i As Integer = 0 To 物料清單_dgv.Rows.Count - 1
            資料數 = 物料清單_dgv.Rows.Count : MyModule1.進度條()
            NU1 = 物料清單_dgv("成本", i).Value : PA1 = 物料清單_dgv("品名", i).Value
            BL1 = 物料清單_dgv("停用", i).Value : PA2 = 物料清單_dgv("料號", i).Value
            SQL_物料檔案_修改() '--------------------資料修改
        Next : conn.Close() : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 : MsgBox("存檔完成。")
    End Sub
    Private Sub 批量刪除_bt_Click(sender As Object, e As EventArgs) Handles 批量刪除_bt.Click
        If 批量刪除選擇_ch.Checked = False Then
            MsgBox("批量刪除選擇的功能要先打勾，並正確選擇要刪除的料號。")
        Else
            Dim aa1 As MsgBoxResult = MsgBox("確定要執行刪除指令嗎?", MsgBoxStyle.OkCancel)
            If aa1 = MsgBoxResult.Ok Then
                批量存檔_bt.Enabled = False : 新增準備_bt.Enabled = True : 存檔_bt.Enabled = True : 刪除_bt.Enabled = True
                批輛停用選擇_ch.Enabled = False : 批輛停用選擇_ch.Checked = False : 啟用批改_ch.Checked = False : 重新整理_tsm.Enabled = True
                新增準備_tsm.Enabled = True : 存檔_tsm.Enabled = True : 刪除_tsm.Enabled = True : 批量刪除選擇_ch.Enabled = False : 批量刪除_bt.Enabled = False
                物料清單_dgv.EditMode = DataGridViewEditMode.EditOnF2 : 狀態_lb.Text = "預覽狀態" '--------------------控件隱藏、操作許可設定與其他基本顯示設定

                For i As Integer = 0 To 物料清單_dgv.Rows.Count - 1
                    資料數 = 物料清單_dgv.Rows.Count : MyModule1.進度條()
                    If 物料清單_dgv("刪除", i).Value = True Then
                        PA20 = 物料清單_dgv("料號", i).Value : SQL_物料檔案_刪除() '--------------------資料刪除
                    End If
                Next : conn.Close() : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0

                MsgBox("刪除完成。") : Set_清單() '--------------------資料修改後重新讀取清單
            End If
        End If
    End Sub
    Private Sub 列印_bt_Click(sender As Object, e As EventArgs) Handles 列印_bt.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, 3) = "雅邸家居-物料清單"
        xlSheet.Cells(2, 1) = "料號" : xlSheet.Cells(2, 2) = "廠商編號" : xlSheet.Cells(2, 3) = "廠商"
        xlSheet.Cells(2, 4) = "品名" : xlSheet.Cells(2, 5) = "盤點數量" : xlSheet.Cells(2, 6) = "備註"
        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
            N1 = i + 3
        Next
        BB(xlApp, xlSheet)
        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        xlSheet.Cells.Select()
        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.Cells.Select()
        With myExcel.Selection.Font
            .Name = "微軟正黑體" : .Size = 12 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With
        xlSheet.Columns("A:F").EntireColumn.AutoFit
        xlSheet.Columns("F:F").Select : myExcel.Selection.ColumnWidth = 20
        xlSheet.Cells.Select() : myExcel.Selection.RowHeight = 25
        xlSheet.Rows("2:2").Select : myExcel.Selection.AutoFilter
        xlSheet.Rows("3:3").Select : myExcel.ActiveWindow.FreezePanes = True

        xlSheet.Range("A1:F1").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False
        End With
        myExcel.Selection.Merge

        xlSheet.Range("A1:F" & N1).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
    End Sub
    '-------------------滑鼠右鍵-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub 重新整理_tsm_Click(sender As Object, e As EventArgs) Handles 重新整理_tsm.Click
        供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear() : 查詢_tb.Text = "" : Set_清單()
    End Sub
    Private Sub 新增準備_tsm_Click(sender As Object, e As EventArgs) Handles 新增準備_tsm.Click
        新增準備_bt.PerformClick()
    End Sub
    Private Sub 存檔_tsm_Click(sender As Object, e As EventArgs) Handles 存檔_tsm.Click
        存檔_bt.PerformClick()
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        物料清單_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
    Private Sub 刪除_tsm_Click(sender As Object, e As EventArgs) Handles 刪除_tsm.Click
        刪除_bt.PerformClick()
    End Sub
End Class