Option Strict Off
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlOrder
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Public Class 列印_樣品貼標
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet : Dim ds1 As New DataSet : Dim ds2 As New DataSet : Dim ds3 As New DataSet
    Dim dr As SqlDataReader
    Dim N1 As Integer : Dim N2 As Integer : Dim N3 As Integer : Dim N4 As Integer : Dim N5 As Integer : Dim N6 As Integer
    Dim N7 As Integer : Dim N8 As Integer : Dim N9 As Integer : Dim N10 As Integer : Dim N11 As Integer
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
    Private Sub Set_DGV1載入前設定()
        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
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單()
        Set_DGV1載入前設定()
        SQL1 = "SELECT SO, 季節, 客戶, 型體號碼, 型體名稱, 國家, 狀態, ISS_DATE, ETA, 數量, 制單人, LEFT(制單時間, 10) AS 制單時間, 版次, 工廠, REQ FROM 樣品訂單控制表 " &
               "WHERE SO LIKE N'" & 列印用SQL & "' AND 版次 LIKE N'" & 列印用SQL2 & "' ORDER BY SO DESC, 版次 DESC"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_DGV2載入前設定()
        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
    End Sub
    Private Sub Set_DGV2載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid2()
    End Sub
    Private Sub Set_清單2()
        Set_DGV2載入前設定()
        SQL1 = "SELECT METOO選擇按鈕.選擇, 樣品訂單部件表.材料名稱, 樣品訂單部件表.材料說明 FROM 樣品訂單部件表 INNER JOIN 部件清單 ON 樣品訂單部件表.部件 = 部件清單.部件 LEFT OUTER JOIN " &
               "METOO選擇按鈕 ON 樣品訂單部件表.部件 = METOO選擇按鈕.對應 WHERE (SO LIKE N'" & 列印用SQL & "') AND (版次 LIKE N'" & 列印用SQL2 & "') AND (流水號 LIKE N'" & TextBox12.Text & "') " &
               "AND (部件清單.貼標對應名稱 LIKE N'" & ComboBox2.Text & "') ORDER BY   樣品訂單部件表.項次, 樣品訂單部件表.部件"
        Set_DGV2載入後設定()
    End Sub
    Private Sub Set_grid2()
        DataGridView2.Columns(0).Width = 40 : DataGridView2.Columns(1).Width = 300 : DataGridView2.Columns(2).Width = 170
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        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.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear() : While (dr.Read()) : ComboBox1.Items.Add(dr("顏色")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox2下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 貼標對應名稱 FROM 貼標對應名稱清單"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox2.Items.Clear() : While (dr.Read()) : ComboBox2.Items.Add(dr("貼標對應名稱")) : End While : conn.Close()
    End Sub
    Private Sub Set_格式設定()
        DataGridView1.Visible = False : TextBox12.Visible = False
        TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox4.Enabled = False : TextBox5.Enabled = False : TextBox6.Enabled = False
        TextBox7.Enabled = False : TextBox8.Enabled = False : TextBox9.Enabled = False : TextBox10.Enabled = False : TextBox11.Enabled = False
    End Sub
    Private Sub 列印_樣品貼標_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
        Set_清單() : Set_清單2() : ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : Set_格式設定()
        TextBox1.Text = DataGridView1.Rows(0).Cells("型體名稱").Value
        TextBox2.Text = DataGridView1.Rows(0).Cells("型體號碼").Value
        TextBox4.Text = DataGridView1.Rows(0).Cells("季節").Value
        TextBox6.Text = DataGridView1.Rows(0).Cells("制單時間").Value
        TextBox7.Text = DataGridView1.Rows(0).Cells("SO").Value
        TextBox8.Text = DataGridView1.Rows(0).Cells("客戶").Value
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT SIZE, 流水號 FROM 樣品訂單清單 WHERE (SO LIKE N'" & 列印用SQL & "' AND 版次 LIKE N'" & 列印用SQL2 & "' AND 顏色 LIKE N'" & ComboBox1.Text & "') ORDER BY 項次"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        If dr.Read() Then
            TextBox5.Text = dr("SIZE")
            TextBox12.Text = dr("流水號")
        End If : conn.Close()
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        If TextBox5.Text = "" Or TextBox12.Text = "" Then
        Else
            Set_清單2()
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                DataGridView2.Rows(i).Cells("選擇").Value = False
            Next
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim II As Integer = 0
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            If DataGridView2.Rows(i).Cells("選擇").Value = True Then
                If ComboBox2.Text = "UPPER" Then
                    TextBox3.Text = TextBox3.Text & II + 1 & ". " & DataGridView2.Rows(i).Cells("材料名稱").Value & "/" & DataGridView2.Rows(i).Cells("材料說明").Value & " " : II = II + 1
                End If
                If ComboBox2.Text = "LINING" Then
                    TextBox9.Text = TextBox9.Text & II + 1 & ". " & DataGridView2.Rows(i).Cells("材料名稱").Value & "/" & DataGridView2.Rows(i).Cells("材料說明").Value & " " : II = II + 1
                End If
                If ComboBox2.Text = "SOCK LINING" Then
                    TextBox10.Text = TextBox10.Text & II + 1 & ". " & DataGridView2.Rows(i).Cells("材料名稱").Value & "/" & DataGridView2.Rows(i).Cells("材料說明").Value & " " : II = II + 1
                End If
                If ComboBox2.Text = "BOTTOM" Then
                    TextBox11.Text = TextBox11.Text & II + 1 & ". " & DataGridView2.Rows(i).Cells("材料名稱").Value & "/" & DataGridView2.Rows(i).Cells("材料說明").Value & " " : II = II + 1
                End If
            End If
        Next
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        TextBox3.Text = ""
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        TextBox9.Text = ""
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        TextBox10.Text = ""
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        TextBox11.Text = ""
    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
        xlBook.Activate()
        xlSheet = NewMethod(xlBook)
        xlSheet.Activate()
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized

        xlSheet.Cells(1, 1) = "Shoe Name : "
        xlSheet.Cells(2, 1) = "Style No : "
        xlSheet.Cells(3, 1) = "Upper : "
        xlSheet.Cells(4, 1) = "Lining : "
        xlSheet.Cells(5, 1) = "Sock Lining : "
        xlSheet.Cells(6, 1) = "Bottom : "
        xlSheet.Cells(7, 1) = "Season : "
        xlSheet.Cells(8, 1) = "Customer : "
        xlSheet.Cells(1, 2) = TextBox1.Text
        xlSheet.Cells(2, 2) = TextBox2.Text
        xlSheet.Cells(3, 2) = TextBox3.Text
        xlSheet.Cells(4, 2) = TextBox9.Text
        xlSheet.Cells(5, 2) = TextBox10.Text
        xlSheet.Cells(6, 2) = TextBox11.Text
        xlSheet.Cells(7, 2) = TextBox4.Text
        xlSheet.Cells(8, 2) = TextBox8.Text
        xlSheet.Cells(7, 3) = "Size : "
        xlSheet.Cells(7, 4) = TextBox5.Text
        xlSheet.Cells(1, 5) = "PURPOSE"
        xlSheet.Cells(2, 5) = "Color : "
        xlSheet.Cells(7, 5) = "Date : "
        xlSheet.Cells(8, 5) = "S/O# : "
        xlSheet.Cells(2, 6) = ComboBox1.Text
        xlSheet.Cells(7, 6) = TextBox6.Text
        xlSheet.Cells(8, 6) = TextBox7.Text
        AA(xlApp, xlSheet)
        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        MsgBox("列印完成")
        Me.Close()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        列印用SQL = "" : Me.Close()
    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 AA(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.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.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("B:D").Select : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("F:F").Select : myExcel.Selection.ColumnWidth = 9
        xlSheet.Rows("1:8").Select : myExcel.Selection.RowHeight = 25

        xlSheet.Range("B2:D2").Select() : 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("B3:F3").Select() : 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("B4:F4").Select() : 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("B5:F5").Select() : 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("B6:F6").Select() : 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

        myExcel.ActiveWindow.DisplayGridlines = False

        'xlSheet.Range("A1:F8").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
        'myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
        'xlSheet.Range("I5").Select()

        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        myExcel.Application.PrintCommunication = True
        myExcel.ActiveSheet.PageSetup.PrintArea = ""
        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
            .LeftMargin = myExcel.Application.InchesToPoints(0.236220472440945)
            .RightMargin = myExcel.Application.InchesToPoints(0.078740157480315)
            .TopMargin = myExcel.Application.InchesToPoints(0.078740157480315)
            .BottomMargin = myExcel.Application.InchesToPoints(0.078740157480315)
            .HeaderMargin = myExcel.Application.InchesToPoints(0)
            .FooterMargin = myExcel.Application.InchesToPoints(0)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintQuality = 203
            .CenterHorizontally = True
            .CenterVertically = True
            .Draft = False
            .PaperSize = 262
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .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 = ""
        End With
    End Sub
End Class