Option Strict Off
Imports System.Data.SqlClient
Public Class METOO專用PAGE導入
    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 ds4 As New DataSet : Dim ds5 As New DataSet : Dim ds6 As New DataSet
    Dim dr As SqlDataReader
    Dim file_name As String = "" : Dim 編號 As String : Dim 顏色編號 As String : Dim 雙數資料 As String : Dim 面料編號 As String
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim N1 As Integer
    Dim 顏色數量 As Integer = 0 : Dim 顏色總數 As Integer = 0 : Dim 面料數量 As Integer = 0
    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 流水號, 季節, 客戶, S_N, C_N, COLOR_NO, COLOR, LOTS, 檔案名稱
                FROM 樣品進度控制表METOO專用 WHERE 檔案名稱 LIKE N'" & file_name & "' ORDER BY COLOR_NO"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 120 : DataGridView1.Columns(1).Width = 100 : DataGridView1.Columns(2).Width = 200 : DataGridView1.Columns(3).Width = 200
        DataGridView1.Columns(4).Width = 200 : DataGridView1.Columns(5).Width = 80 : DataGridView1.Columns(6).Width = 320 : DataGridView1.Columns(7).Width = 320
        DataGridView1.Columns(8).Width = 320
        DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub
    Private Sub Set_清單1()
        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 樣品進度控制表METOO專用.流水號, 樣品進度控制表METOO專用.COLOR_NO, 樣品進度表UPPER資料METOO專用.UPPER_NO, 樣品進度表UPPER資料METOO專用.UPPER, 樣品進度表UPPER資料METOO專用.ARTIDE, 
                       樣品進度表UPPER資料METOO專用.COLOR 
                FROM 樣品進度控制表METOO專用 RIGHT OUTER JOIN 樣品進度表UPPER資料METOO專用 ON 樣品進度控制表METOO專用.流水號 = 樣品進度表UPPER資料METOO專用.流水號 AND 
                            樣品進度控制表METOO專用.COLOR_NO = 樣品進度表UPPER資料METOO專用.COLOR_NO
                WHERE (樣品進度控制表METOO專用.檔案名稱 LIKE N'" & file_name & "')
                ORDER BY 樣品進度控制表METOO專用.COLOR_NO"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid1()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Width = 120 : DataGridView2.Columns(1).Width = 100 : DataGridView2.Columns(2).Width = 100 : DataGridView2.Columns(3).Width = 345
        DataGridView2.Columns(4).Width = 200 : DataGridView2.Columns(5).Width = 346
        DataGridView2.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub
    Private Sub Set_清單2()
        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 樣品進度控制表METOO專用.流水號, 樣品進度控制表METOO專用.COLOR_NO, 樣品進度表其他部件資料METOO專用.Descriptin_N, 樣品進度表其他部件資料METOO專用.Description
                FROM 樣品進度控制表METOO專用 INNER JOIN 樣品進度表其他部件資料METOO專用 ON 樣品進度控制表METOO專用.流水號 = 樣品進度表其他部件資料METOO專用.流水號 AND 
                     樣品進度控制表METOO專用.COLOR_NO = 樣品進度表其他部件資料METOO專用.COLOR_NO
                WHERE (樣品進度控制表METOO專用.檔案名稱 LIKE N'" & file_name & "')
                ORDER BY 樣品進度控制表METOO專用.COLOR_NO"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView3.DataSource = ds2.Tables(0) : conn.Close() : Set_grid2()
    End Sub
    Private Sub Set_grid2()
        DataGridView3.Columns(0).Width = 120 : DataGridView3.Columns(1).Width = 100 : DataGridView3.Columns(2).Width = 100 : DataGridView3.Columns(3).Width = 300
        DataGridView3.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub
    Private Sub ComboBox7下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT SEASON FROM 季節清單 ORDER BY SEASON"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox7.Items.Clear() : While (dr.Read()) : ComboBox7.Items.Add(dr("SEASON")) : 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 客戶控制表 ORDER BY 客戶簡稱"
        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 METOO專用PAGE導入_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        ComboBox7下拉表單資料載入() : ComboBox2下拉表單資料載入()
    End Sub
    Private Sub DataGridView2_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView2.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView2.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView2(0, e.RowIndex).Value.ToString <> DataGridView2(0, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView2.RowHeadersVisible, DataGridView2.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView2.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView2.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub DataGridView3_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView3.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView3.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView3(0, e.RowIndex).Value.ToString <> DataGridView3(0, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView3.RowHeadersVisible, DataGridView3.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView3.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView3.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ComboBox2.Text = "" Or ComboBox7.Text = "" Then
            MsgBox("季節與客戶不可空白")
        Else
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then : file_name = OpenFileDialog1.FileName : End If
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT TOP (1) 檔案名稱 FROM 樣品進度控制表METOO專用 WHERE 檔案名稱 LIKE N'" & file_name & "'"
            cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
            If dr.Read() Then : MsgBox("該檔案已入過系統") : conn.Close()
            Else
                conn.Close()
                xlApp = CreateObject("Excel.Application")
                xlApp.DisplayAlerts = False
                xlApp.Visible = True
                Dim xlBook As Object = xlApp.Workbooks.Open(file_name)
                N1 = xlBook.Sheets.Count

                For i As Integer = 1 To N1
                    If Strings.Left(xlBook.Sheets(i).Name, 4) = "PAGE" Then
                        xlSheet = xlApp.Worksheets(i)
                        xlSheet.Activate()
                        顏色數量 = 0
                        For ii As Integer = 3 To 10
                            If xlSheet.Cells(29, ii).value <> "" Then
                                顏色數量 = 顏色數量 + 1 : 顏色總數 = 顏色總數 + 1

                                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                                SQL1 = "SELECT TOP (1) 流水號 FROM 樣品進度控制表METOO專用 ORDER BY 流水號 DESC"
                                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                                If dr.Read() Then : 編號 = dr("流水號").ToString : Else : 編號 = "PG00000000" : End If
                                conn.Close()
                                Dim NUM1 As Integer = Double.Parse(Strings.Right(編號, 8)) + 1
                                If NUM1 < 10 Then : 編號 = "PG" & "0000000" & NUM1 : ElseIf NUM1 > 9 And NUM1 < 100 Then : 編號 = "PG" & "000000" & NUM1
                                ElseIf NUM1 > 99 And NUM1 < 1000 Then : 編號 = "PG" & "00000" & NUM1 : ElseIf NUM1 > 999 And NUM1 < 10000 Then : 編號 = "PG" & "0000" & NUM1
                                ElseIf NUM1 > 9999 And NUM1 < 100000 Then : 編號 = "PG" & "000" & NUM1 : ElseIf NUM1 > 99999 And NUM1 < 100000 Then : 編號 = "PG" & "00" & NUM1
                                ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : 編號 = "PG" & "0" & NUM1 : ElseIf NUM1 > 9999999 Then : 編號 = "PG" & NUM1 : End If

                                If 顏色總數 < 10 Then : 顏色編號 = "C0" & 顏色總數 : Else : 顏色編號 = "C" & 顏色總數 : End If

                                For iii As Integer = 29 To 999
                                    If Strings.Left(xlSheet.Cells(iii, 2).value, 4) = "LOTS" Then : 雙數資料 = xlSheet.Cells(iii, ii).value : iii = 999 : End If
                                Next

                                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                                SQL1 = "INSERT INTO 樣品進度控制表METOO專用 (流水號, 季節, 客戶, S_N, C_N, COLOR_NO, COLOR, LOTS, 檔案名稱) " &
                                       "VALUES ('" & 編號 & "',N'" & ComboBox7.Text & "',N'" & ComboBox2.Text & "',N'" & xlSheet.Cells(2, 3).value & "',N'" & xlSheet.Cells(3, 3).value &
                                             "',N'" & 顏色編號 & "',N'" & xlSheet.Cells(29, ii).value & "',N'" & 雙數資料 & "',N'" & file_name & "')"
                                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                                面料數量 = 0
                                For iii As Integer = 29 To 999
                                    If Strings.Left(xlSheet.Cells(iii, 2).value, 5) = "UPPER" Or Strings.Left(xlSheet.Cells(iii, 2).value, 5) = "Upper" Or
                                        Strings.Left(xlSheet.Cells(iii, 2).value, 5) = "upper" Then
                                        面料數量 = 面料數量 + 1
                                        If 面料數量 < 10 Then : 面料編號 = "UP0" & 面料數量 : Else : 面料編號 = "UP" & 面料數量 : End If

                                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                                        SQL1 = "INSERT INTO 樣品進度表UPPER資料METOO專用 (流水號, COLOR_NO, UPPER_NO, UPPER, ARTIDE, COLOR) " &
                                               "VALUES ('" & 編號 & "',N'" & 顏色編號 & "',N'" & 面料編號 &
                                               "',N'" & xlSheet.Cells(iii, ii).value & "',N'" & xlSheet.Cells(iii + 1, ii).value & "',N'" & xlSheet.Cells(iii + 2, ii).value & "')"
                                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                                    ElseIf Strings.Left(xlSheet.Cells(iii, 2).value, 4) = "LOTS" Then
                                        iii = 999
                                    End If
                                Next

                                For iii As Integer = 29 To 999
                                    If Strings.Left(xlSheet.Cells(iii, 2).value, 4) = "" Then
                                    ElseIf Strings.Left(xlSheet.Cells(iii, 2).value, 4) = "LOTS" Then
                                        iii = 999
                                    ElseIf Strings.Left(xlSheet.Cells(iii, 2).value, 5) <> "UPPER" And Strings.Left(xlSheet.Cells(iii, 2).value, 5) <> "Upper" And
                                            Strings.Left(xlSheet.Cells(iii, 2).value, 5) <> "upper" And Strings.Left(xlSheet.Cells(iii, 2).value, 7) <> "Article" And
                                            Strings.Left(xlSheet.Cells(iii, 2).value, 5) <> "Color" And Strings.Left(xlSheet.Cells(iii, 2).value, 4) <> "LOTS" Then
                                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                                        SQL1 = "INSERT INTO 樣品進度表其他部件資料METOO專用 (流水號, COLOR_NO, Descriptin_N, Description) " &
                                               "VALUES ('" & 編號 & "',N'" & 顏色編號 & "',N'" & xlSheet.Cells(iii, 2).value & "',N'" & xlSheet.Cells(iii, ii).value & "')"
                                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                                    End If
                                Next iii
                            Else
                                ii = 10
                            End If
                        Next
                    End If
                Next
                Set_清單() : Set_清單1() : Set_清單2()
                xlApp.Visible = False
            End If
            MsgBox("導入顏色數量為 " & 顏色總數 & " 筆")
            N1 = 0 : 顏色數量 = 0 : 顏色總數 = 0 : 面料數量 = 0
        End If
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        xlApp.Visible = True
    End Sub
    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView3.DataSource = Nothing : ds2.Clear()
        xlApp.Visible = True : xlApp.Quit() : xlApp = Nothing
    End Sub
End Class