Option Strict Off
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 System.Threading
Imports Microsoft.Office.Interop.Excel
Public Class 列印_樣品單_NEW
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet : ReadOnly ds3 As New DataSet : ReadOnly ds5 As New DataSet
    Dim N1 As Integer : Dim N2 As Integer : Dim N3 As Integer : Dim N4 As Integer : Dim N5 As Integer
    Dim xlApp As Application : Dim xlBook As Workbook : Dim xlSheet As Worksheet
    Dim Q1 As Integer : Dim AAA1 As String : Dim AAA2 As String
    Private Sub Set_清單()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT      樣品訂單控制表.SO, 樣品訂單控制表.季節, 樣品訂單控制表.客戶, 樣品訂單控制表.型體號碼, 
                            樣品訂單控制表.型體名稱, 樣品訂單控制表.國家, 樣品訂單控制表.狀態, 樣品訂單控制表.ISS_DATE, 
                            樣品訂單控制表.ETA, 樣品訂單控制表.數量, 樣品訂單控制表.制單人, LEFT(樣品訂單控制表.制單時間, 10) 
                            AS 制單時間, 樣品訂單控制表.版次, 樣品訂單控制表.工廠, 樣品訂單控制表.REQ, 樣品訂單控制表.C_N, 
                            樣品訂單控制表.中底, 樣品訂單控制表.楦頭編號, 楦頭規格控制表.楦頭名, 模具開發控制表.模具編號, 
                            模具開發控制表.CN
                FROM        樣品訂單控制表 LEFT OUTER JOIN 模具開發控制表 ON 樣品訂單控制表.模具編號 = 模具開發控制表.模具編號 LEFT OUTER JOIN
                            楦頭規格控制表 ON 樣品訂單控制表.楦頭編號 = 楦頭規格控制表.楦頭編號
                WHERE      (樣品訂單控制表.SO LIKE N'" & 列印用SQL & "') AND (樣品訂單控制表.版次 LIKE N'" & 列印用SQL2 & "')
                GROUP BY    樣品訂單控制表.SO, 樣品訂單控制表.季節, 樣品訂單控制表.客戶, 樣品訂單控制表.型體號碼, 
                            樣品訂單控制表.型體名稱, 樣品訂單控制表.國家, 樣品訂單控制表.狀態, 樣品訂單控制表.ISS_DATE, 
                            樣品訂單控制表.ETA, 樣品訂單控制表.數量, 樣品訂單控制表.制單人, LEFT(樣品訂單控制表.制單時間, 10), 
                            樣品訂單控制表.版次, 樣品訂單控制表.工廠, 樣品訂單控制表.REQ, 樣品訂單控制表.C_N, 樣品訂單控制表.中底, 
                            樣品訂單控制表.楦頭編號, 楦頭規格控制表.楦頭名, 模具開發控制表.模具編號, 模具開發控制表.CN
                ORDER BY    樣品訂單控制表.SO DESC, 樣品訂單控制表.版次 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單2()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 項次, 顏色, 架號, 數量, SIZE, KEEP, 流水號, 版次, 備註, 借出者 FROM 樣品訂單清單 WHERE (SO LIKE N'" & 列印用SQL & "' AND 版次 LIKE N'" & 列印用SQL2 & "') ORDER BY 項次"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單3()
        DataGridView3.DataSource = Nothing : ds2.Clear()
        DataGridView3.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView3.ColumnHeadersHeight = 25
        DataGridView3.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 部件 FROM 樣品訂單部件表 WHERE (SO LIKE N'" & 列印用SQL & "') AND (版次 LIKE N'" & 列印用SQL2 & "') GROUP BY   部件 ORDER BY   部件"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView3.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單4()
        DataGridView4.DataSource = Nothing : ds3.Clear()
        DataGridView4.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView4.ColumnHeadersHeight = 25
        DataGridView4.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT SO, 項次, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號 FROM 樣品訂單部件表 WHERE (SO LIKE N'" & 列印用SQL & "') AND (版次 LIKE N'" & 列印用SQL2 & "') ORDER BY   流水號, 部件"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds3) : DataGridView4.DataSource = ds3.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單5()
        DataGridView5.DataSource = Nothing : ds5.Clear()
        DataGridView5.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView5.ColumnHeadersHeight = 25
        DataGridView5.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 項次, 備註 FROM 樣品訂單清單 WHERE (SO LIKE N'" & 列印用SQL & "' AND 版次 LIKE N'" & 列印用SQL2 & "') ORDER BY 項次"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds5) : DataGridView5.DataSource = ds5.Tables(0) : conn.Close()
    End Sub
    Private Sub 列印_樣品單_NEW_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
        N1 = 0 : N2 = 0 : N3 = 0 : N4 = 0 : N5 = 0 : Q1 = 0 : AAA1 = "" : AAA2 = ""
        Set_清單() : Set_清單2() : Set_清單3() : Set_清單4() : Set_清單5()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        列印用SQL = "" : 列印用SQL2 = "" : Me.Close()
    End Sub
    Private Sub 列印1()
        xlSheet.Cells(1, 1) = "森康鞋業有限公司  SenKang Footwear Ltd."
        If DataGridView1.Rows(0).Cells("狀態").Value.ToString = "DEVELOPMENT" Then
            xlSheet.Cells(1, 4) = "DEVELOPMENT SAMPLE ORDER (開發樣品單)"
        ElseIf DataGridView1.Rows(0).Cells("狀態").Value.ToString = "SALES" Then
            xlSheet.Cells(1, 4) = "SALES SAMPLE ORDER (銷樣樣品單)"
        ElseIf DataGridView1.Rows(0).Cells("狀態").Value.ToString = "CONFIRMATION" Then
            xlSheet.Cells(1, 4) = "CONFIRMATION SAMPLE ORDER (確認樣品單)"
        ElseIf DataGridView1.Rows(0).Cells("狀態").Value.ToString = "SHOW" Then
            xlSheet.Cells(1, 4) = "SHOW SAMPLE ORDER (秀展樣品單)"
        ElseIf DataGridView1.Rows(0).Cells("狀態").Value.ToString = "TRIAL " Then
            xlSheet.Cells(1, 4) = "TRIAL SAMPLE ORDER (試穿樣品單)"
        End If
        xlSheet.Cells(2, 1) = "廣東東莞市厚街鎮湖景大道南1號" : xlSheet.Cells(3, 1) = "TEL:0769-38937740"
        xlSheet.Cells(3, 4) = "PRINT DATE :" : xlSheet.Cells(3, 5) = "=TODAY()"

        xlSheet.Cells(5, 1) = "S/O # (單號) :" : xlSheet.Cells(6, 1) = "CUST (客戶) :" : xlSheet.Cells(7, 1) = "FACTORY (工廠) :" : xlSheet.Cells(8, 1) = "STYLE # (型體號) :"
        xlSheet.Cells(9, 1) = "STY.NAME (型體名) :" : xlSheet.Cells(10, 1) = "COUNTRY (國家) :" : xlSheet.Cells(11, 1) = "PURPOSE (目的) :" : xlSheet.Cells(12, 1) = "楦頭編號/模具編號 :"
        xlSheet.Cells(5, 2) = DataGridView1.Rows(0).Cells("SO").Value : xlSheet.Cells(6, 2) = DataGridView1.Rows(0).Cells("客戶").Value
        xlSheet.Cells(7, 2) = DataGridView1.Rows(0).Cells("工廠").Value : xlSheet.Cells(8, 2) = DataGridView1.Rows(0).Cells("型體號碼").Value
        xlSheet.Cells(9, 2) = DataGridView1.Rows(0).Cells("型體名稱").Value : xlSheet.Cells(10, 2) = DataGridView1.Rows(0).Cells("國家").Value
        xlSheet.Cells(11, 2) = DataGridView1.Rows(0).Cells("狀態").Value : xlSheet.Cells(12, 2) = DataGridView1.Rows(0).Cells("楦頭編號").Value & "/" & DataGridView1.Rows(0).Cells("模具編號").Value

        xlSheet.Cells(5, 3) = "EDITION (版次) :" : xlSheet.Cells(6, 3) = "SEASON (季節) :" : xlSheet.Cells(7, 3) = "ISS. DATE (制單日期) :" : xlSheet.Cells(8, 3) = "ETD (預計寄出日期) :"
        xlSheet.Cells(9, 3) = "REQ. BY :" : xlSheet.Cells(10, 3) = "INVOICE :" : xlSheet.Cells(11, 3) = "TOTAL (總數) :" : xlSheet.Cells(12, 3) = "Construction Name :"
        xlSheet.Cells(5, 4) = DataGridView1.Rows(0).Cells("版次").Value : xlSheet.Cells(6, 4) = DataGridView1.Rows(0).Cells("季節").Value
        xlSheet.Cells(7, 4) = DataGridView1.Rows(0).Cells("ISS_DATE").Value : xlSheet.Cells(8, 4) = DataGridView1.Rows(0).Cells("ETA").Value
        xlSheet.Cells(9, 4) = DataGridView1.Rows(0).Cells("REQ").Value : xlSheet.Cells(10, 4) = ""
        xlSheet.Cells(11, 4) = DataGridView1.Rows(0).Cells("數量").Value : xlSheet.Cells(12, 4) = DataGridView1.Rows(0).Cells("CN").Value

        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 中皮, 結構 FROM 中皮結構 WHERE 底台 LIKE N'" & DataGridView1.Rows(0).Cells("C_N").Value & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : AAA1 = dr("中皮") : AAA2 = dr("結構") : End If : conn.Close()

        xlSheet.Cells(13, 1) = "中皮 : " & DataGridView1.Rows(0).Cells("C_N").Value : xlSheet.Cells(13, 2) = AAA1 : xlSheet.Cells(13, 4) = AAA2

        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 做法 FROM 中底做法 WHERE 中底 LIKE N'" & DataGridView1.Rows(0).Cells("中底").Value & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : AAA1 = dr("做法") : End If : conn.Close()

        xlSheet.Cells(14, 1) = "中底 : " & DataGridView1.Rows(0).Cells("中底").Value : xlSheet.Cells(14, 2) = AAA1

        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'" & DataGridView1.Rows(0).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(0).Cells("型體號碼").Value &
                       "' AND 形體名 LIKE N'" & DataGridView1.Rows(0).Cells("型體名稱").Value & "' AND 類別 LIKE N'設計圖'"
        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'" & DataGridView1.Rows(0).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(0).Cells("型體號碼").Value &
                           "' AND 形體名 LIKE N'" & DataGridView1.Rows(0).Cells("型體名稱").Value & "' AND 類別 LIKE N'設計圖'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            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) : 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'" & DataGridView1.Rows(0).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(0).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'" & DataGridView1.Rows(0).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(0).Cells("型體號碼").Value & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                While dr.Read() = True
                    Dim unused1 As Byte() = New Byte(-1) {} : Dim bytes As Byte() = 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 圖片 FROM 鞋樣素描圖控制表 WHERE 型體編號 LIKE N'" & DataGridView1.Rows(0).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 圖片 FROM 鞋樣素描圖控制表 WHERE 型體編號 LIKE N'" & DataGridView1.Rows(0).Cells("型體號碼").Value & "'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    While dr.Read() = True
                        Dim unused2 As Byte() = New Byte(-1) {} : Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                        Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                Else
                    conn.Close()
                End If
            End If
        End If : PictureBox1.SizeMode = 4
        For ii As Integer = 0 To 20 : If PictureBox1.Image Is Nothing Then : Thread.Sleep(100) : Else : AA(xlApp, xlSheet) : ii = 20 : End If : Next ii
        If PictureBox1.Image Is Nothing Then : xlSheet.Cells(5, 5) = "系統中無圖片" : End If

        xlSheet.Cells(16, 1) = "Color (顏色) :" : xlSheet.Cells(17, 1) = "QTY (數量) :" : xlSheet.Cells(18, 1) = "SIZE (尺寸) :"
        N1 = 0
        For i As Integer = 0 To DataGridView3.Rows.Count - 1
            xlSheet.Cells(i + 19, 1) = DataGridView3.Rows(i).Cells("部件").Value
            N1 = i
        Next
        BB(xlApp, xlSheet) : CC(xlApp, xlSheet)

        If N2 > DataGridView2.Rows.Count - 1 Then
            N3 = DataGridView2.Rows.Count - 1
        Else
            N3 = N2
        End If
        Dim QQAA As String : Dim WWSS As String
        If N2 = 3 Then
            For i As Integer = 0 To N3
                xlSheet.Cells(16, i + 2) = DataGridView2.Rows(i).Cells("顏色").Value
                xlSheet.Cells(17, i + 2) = DataGridView2.Rows(i).Cells("數量").Value
                xlSheet.Cells(18, i + 2) = DataGridView2.Rows(i).Cells("SIZE").Value
                QQAA = DataGridView2.Rows(i).Cells("流水號").Value
                For ii As Integer = 0 To DataGridView3.Rows.Count - 1
                    WWSS = DataGridView3.Rows(ii).Cells("部件").Value
                    For iii As Integer = 0 To DataGridView4.Rows.Count - 1
                        If QQAA = DataGridView4.Rows(iii).Cells("流水號").Value And WWSS = DataGridView4.Rows(iii).Cells("部件").Value Then
                            If DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                     "-" & DataGridView4.Rows(iii).Cells("材料說明").Value & "-" & DataGridView4.Rows(iii).Cells("備註").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("材料說明").Value & "-" & DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("材料說明").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("備註").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("備註").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                "-" & DataGridView4.Rows(iii).Cells("材料說明").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                "-" & DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value &
                                "-" & DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value &
                                "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("備註").Value &
                                "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, i + 2) = DataGridView4.Rows(iii).Cells("廠商").Value
                            End If
                        End If
                    Next
                Next
            Next
        ElseIf N2 > 3 Then
            N4 = 0
            For i As Integer = 4 To N3
                xlSheet.Cells(16, N4 + 2) = DataGridView2.Rows(i).Cells("顏色").Value
                xlSheet.Cells(17, N4 + 2) = DataGridView2.Rows(i).Cells("數量").Value
                xlSheet.Cells(18, N4 + 2) = DataGridView2.Rows(i).Cells("SIZE").Value
                QQAA = DataGridView2.Rows(i).Cells("流水號").Value
                For ii As Integer = 0 To DataGridView3.Rows.Count - 1
                    WWSS = DataGridView3.Rows(ii).Cells("部件").Value
                    For iii As Integer = 0 To DataGridView4.Rows.Count - 1
                        If QQAA = DataGridView4.Rows(iii).Cells("流水號").Value And WWSS = DataGridView4.Rows(iii).Cells("部件").Value Then
                            If DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                     "-" & DataGridView4.Rows(iii).Cells("材料說明").Value & "-" & DataGridView4.Rows(iii).Cells("備註").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("材料說明").Value & "-" & DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("材料說明").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("備註").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value &
                                       "-" & DataGridView4.Rows(iii).Cells("備註").Value & "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                "-" & DataGridView4.Rows(iii).Cells("材料說明").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                "-" & DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value &
                                "-" & DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value &
                                "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value &
                                "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("備註").Value &
                                "-" & DataGridView4.Rows(iii).Cells("廠商").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料名稱").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString <> "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("材料說明").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString <> "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString = "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("備註").Value
                            ElseIf DataGridView4.Rows(iii).Cells("材料名稱").Value.ToString = "" And DataGridView4.Rows(iii).Cells("材料說明").Value.ToString = "" And
                            DataGridView4.Rows(iii).Cells("備註").Value.ToString = "" And DataGridView4.Rows(iii).Cells("廠商").Value.ToString <> "" Then
                                xlSheet.Cells(ii + 19, N4 + 2) = DataGridView4.Rows(iii).Cells("廠商").Value
                            End If
                        End If
                    Next
                Next
                N4 += 1
            Next
        End If

        For i As Integer = 0 To DataGridView5.Rows.Count - 1
            xlSheet.Cells(N1 + 20 + i, 1) = "備註" & i + 1 & ":"
            xlSheet.Cells(N1 + 20 + i, 2) = DataGridView5.Rows(i).Cells("備註").Value.ToString
            N5 = N1 + 20 + i
        Next
        FF(xlApp, xlSheet) : DD(xlApp, xlSheet)
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
        xlBook = xlApp.Workbooks.Add
        xlApp.DisplayAlerts = True
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized
        N2 = 0
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            If i = 0 Then : xlSheet = NewMethod(xlBook) : xlApp.Sheets(1).Name = "第一頁" : N2 = 3 : xlBook.Activate() : xlSheet.Activate() : 列印1() : Q1 = 0
            ElseIf i = 4 Then : xlSheet = NewMethod(xlBook) : xlApp.Sheets(1).Name = "第二頁" : N2 = 7 : xlBook.Activate() : xlSheet.Activate() : 列印1() : Q1 = 0
            ElseIf i = 8 Then : xlSheet = NewMethod(xlBook) : xlApp.Sheets(1).Name = "第三頁" : N2 = 11 : xlBook.Activate() : xlSheet.Activate() : 列印1() : Q1 = 0
            ElseIf i = 12 Then : xlSheet = NewMethod(xlBook) : xlApp.Sheets(1).Name = "第四頁" : N2 = 15 : xlBook.Activate() : xlSheet.Activate() : 列印1() : Q1 = 0
            End If
        Next

        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        MsgBox("列印完成")
        Me.Close()
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        Clipboard.SetDataObject(PictureBox1.Image)
        xlSheet.Cells(1, 1).Select
        Thread.Sleep(100) '延迟0.1秒
        xlSheet.Range("E5").Select()
        myExcel.ActiveSheet.Paste
        If PictureBox1.Image.Height < PictureBox1.Image.Width Then
            myExcel.Selection.ShapeRange.Width = 120
        Else
            myExcel.Selection.ShapeRange.Height = 120
        End If
        myExcel.Selection.ShapeRange.IncrementLeft(2)
        myExcel.Selection.ShapeRange.IncrementTop(2)
    End Sub
    Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        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:E").Select : myExcel.Selection.ColumnWidth = 30
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("A1:C1").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 20 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With

        xlSheet.Range("D1:E1").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
        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.Range("A2:C2").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("A3:C3").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.Rows("1:1").Select : myExcel.Selection.RowHeight = 35 : xlSheet.Rows("2:3").Select : myExcel.Selection.RowHeight = 20
        xlSheet.Rows("4:4").Select : myExcel.Selection.RowHeight = 5 : xlSheet.Rows("5:12").Select : myExcel.Selection.RowHeight = 20
        xlSheet.Rows("13:13").Select : myExcel.Selection.RowHeight = 45 : xlSheet.Rows("14:14").Select : myExcel.Selection.RowHeight = 20

        xlSheet.Range("A3:E3").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
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("A12:E14").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
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("E5:E12").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
        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
        myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Rows("15:15").Select : myExcel.Selection.RowHeight = 5

        xlSheet.Range("A5:A12").Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent1 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0
        End With

        xlSheet.Range("C5:C13").Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent1 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0 : End With
        myExcel.ActiveWindow.DisplayGridlines = False

        xlSheet.Range("B13:C13").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("D13:E13").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("B14:E14").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("A13:E14").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.Range("A13").Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False
        End With
    End Sub
    Private Sub CC(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Rows("16:16").Select : myExcel.Selection.RowHeight = 30
        xlSheet.Rows("17:18").Select : myExcel.Selection.RowHeight = 20
        xlSheet.Rows("19:" & N1 + 19).Select : myExcel.Selection.RowHeight = 60

        xlSheet.Range("A16:E" & N1 + 19).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("B16:E16").Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent2 : .TintAndShade = 0.599993896298105 : .PatternTintAndShade = 0 : End With

        xlSheet.Range("A16:A" & N1 + 19).Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .Color = 65535 : .TintAndShade = 0 : .PatternTintAndShade = 0 : End With
    End Sub
    Private Sub FF(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Rows(N1 + 20 & ":" & N5).Select : myExcel.Selection.RowHeight = 30

        xlSheet.Range("A" & N1 + 20 & ":E" & N5).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("A" & N1 + 20 & ":A" & N5).Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent1 : .TintAndShade = 0.599993896298105 : .PatternTintAndShade = 0 : End With

        For I As Integer = N1 + 20 To N5
            xlSheet.Range("B" & I & ":E" & I).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
        Next

        xlSheet.Range("B" & N1 + 20 & ":E" & N5).Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : End With
        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
    End Sub
    Private Sub DD(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Range("B19:E" & N1 + 19).Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
        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

        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
End Class