Option Strict Off
Imports System.Data.SqlClient
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 Microsoft.Office.Interop.Excel
Public Class 列印_樣品報價單
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim N1 As Integer : Dim N3 As Integer : Dim N4 As Integer
    Dim xlApp As Application
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    Dim 位置1 As String
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds1.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(ds1) : DataGridView1.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_訂單清單1()
        Set_DGV1載入前設定()
        SQL1 = 列印用SQL
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_格式設定()
        DataGridView1.Visible = False : DataGridView2.Visible = False
        PictureBox1.Visible = False
    End Sub
    Private Sub 列印_樣品報價單_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Set_格式設定()
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
        Set_訂單清單1()
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.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, 1) = "WTL ENTERPRISE CO., LTD" : xlSheet.Cells(2, 1) = "PRICING LINESHEET-lot" : xlSheet.Cells(3, 1) = "SKETCH" : xlSheet.Cells(3, 2) = "SEASON" : xlSheet.Cells(3, 3) = "CUST"
        xlSheet.Cells(3, 4) = "FCTY" : xlSheet.Cells(3, 5) = "STYLE" : xlSheet.Cells(3, 6) = "STYLENAME" : xlSheet.Cells(3, 7) = "COLOR" : xlSheet.Cells(3, 8) = "SO" : xlSheet.Cells(3, 9) = "Edition"
        xlSheet.Cells(3, 10) = "Item" : xlSheet.Cells(3, 11) = "MATERIALS" : xlSheet.Cells(3, 12) = "MATERIAL_CLASS" : xlSheet.Cells(3, 13) = "OUTSOLE" : xlSheet.Cells(3, 14) = "INSOLE"
        xlSheet.Cells(3, 15) = "FC" : xlSheet.Cells(3, 16) = "REMARK"
        AA(xlApp, xlSheet)

        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            If i = 0 Then
                xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("SEASON").Value : xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("CUST").Value
                xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("FCTY").Value : xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("STYLE").Value
                xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("STYLENAME").Value : xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("COLOR").Value
                xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("SO").Value : xlSheet.Cells(i + 4, 9) = DataGridView1.Rows(i).Cells("版次").Value
                xlSheet.Cells(i + 4, 10) = DataGridView1.Rows(i).Cells("項次").Value : xlSheet.Cells(i + 4, 11) = DataGridView1.Rows(i).Cells("MATERIALS").Value
                xlSheet.Cells(i + 4, 12) = DataGridView1.Rows(i).Cells("MATERIAL_CLASS").Value : xlSheet.Cells(i + 4, 13) = DataGridView1.Rows(i).Cells("OUTSOLE").Value
                xlSheet.Cells(i + 4, 14) = DataGridView1.Rows(i).Cells("INSOLE").Value : xlSheet.Cells(i + 4, 15) = DataGridView1.Rows(i).Cells("FC").Value
                xlSheet.Cells(i + 4, 16) = DataGridView1.Rows(i).Cells("REMARK").Value
                N3 = i + 4 : N4 = i + 4
                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(i).Cells("CUST").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(i).Cells("STYLE").Value &
                       "' AND 形體名 LIKE N'" & DataGridView1.Rows(i).Cells("STYLENAME").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(i).Cells("CUST").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(i).Cells("STYLE").Value &
                           "' AND 形體名 LIKE N'" & DataGridView1.Rows(i).Cells("STYLENAME").Value & "' AND 類別 LIKE N'設計圖'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    While dr.Read() = True
                        Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close() : GG(xlApp, xlSheet)
                Else
                    xlSheet.Cells(i + 4, 1) = "系統中無圖片"
                    conn.Close()
                End If : PictureBox1.SizeMode = 4
            ElseIf DataGridView1.Rows(i - 1).Cells("SO").Value = DataGridView1.Rows(i).Cells("SO").Value And DataGridView1.Rows(i - 1).Cells("版次").Value = DataGridView1.Rows(i).Cells("版次").Value Then
                xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("COLOR").Value : xlSheet.Cells(i + 4, 10) = DataGridView1.Rows(i).Cells("項次").Value
                xlSheet.Cells(i + 4, 11) = DataGridView1.Rows(i).Cells("MATERIALS").Value : xlSheet.Cells(i + 4, 12) = DataGridView1.Rows(i).Cells("MATERIAL_CLASS").Value
                xlSheet.Cells(i + 4, 13) = DataGridView1.Rows(i).Cells("OUTSOLE").Value : xlSheet.Cells(i + 4, 14) = DataGridView1.Rows(i).Cells("INSOLE").Value
                xlSheet.Cells(i + 4, 15) = DataGridView1.Rows(i).Cells("FC").Value : xlSheet.Cells(i + 4, 16) = DataGridView1.Rows(i).Cells("REMARK").Value
                N4 = i + 4
            ElseIf DataGridView1.Rows(i - 1).Cells("SO").Value <> DataGridView1.Rows(i).Cells("SO").Value Or DataGridView1.Rows(i - 1).Cells("版次").Value <> DataGridView1.Rows(i).Cells("版次").Value Then
                CC(xlApp, xlSheet)
                xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("SEASON").Value : xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("CUST").Value
                xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("FCTY").Value : xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("STYLE").Value
                xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("STYLENAME").Value : xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("COLOR").Value
                xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("SO").Value : xlSheet.Cells(i + 4, 9) = DataGridView1.Rows(i).Cells("版次").Value
                xlSheet.Cells(i + 4, 10) = DataGridView1.Rows(i).Cells("項次").Value : xlSheet.Cells(i + 4, 11) = DataGridView1.Rows(i).Cells("MATERIALS").Value
                xlSheet.Cells(i + 4, 12) = DataGridView1.Rows(i).Cells("MATERIAL_CLASS").Value : xlSheet.Cells(i + 4, 13) = DataGridView1.Rows(i).Cells("OUTSOLE").Value
                xlSheet.Cells(i + 4, 14) = DataGridView1.Rows(i).Cells("INSOLE").Value : xlSheet.Cells(i + 4, 15) = DataGridView1.Rows(i).Cells("FC").Value
                xlSheet.Cells(i + 4, 16) = DataGridView1.Rows(i).Cells("REMARK").Value
                N3 = i + 4 : N4 = i + 4
                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(i).Cells("CUST").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(i).Cells("STYLE").Value &
                       "' AND 形體名 LIKE N'" & DataGridView1.Rows(i).Cells("STYLENAME").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(i).Cells("CUST").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(i).Cells("STYLE").Value &
                           "' AND 形體名 LIKE N'" & DataGridView1.Rows(i).Cells("STYLENAME").Value & "' AND 類別 LIKE N'設計圖'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    While dr.Read() = True
                        Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close() : GG(xlApp, xlSheet)
                Else
                    xlSheet.Cells(i + 4, 1) = "系統中無圖片"
                    conn.Close()
                End If : PictureBox1.SizeMode = 4
            End If
            With xlApp.Selection
                .Font.Bold = True
            End With
            N1 = i + 4
        Next i
        CC(xlApp, xlSheet) : BB(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 Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 9 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlTop : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
        myExcel.Selection.RowHeight = 45

        xlSheet.Range("A1").Select() : With myExcel.Selection.Font : .Size = 22 : End With
        xlSheet.Range("A2").Select() : With myExcel.Selection.Font : .Size = 18 : End With

        xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 30 : xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 25 : xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 30
        xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 8.25
        xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("C:G").Select : myExcel.Selection.ColumnWidth = 10
        xlSheet.Columns("H:H").Select : myExcel.Selection.ColumnWidth = 6.75
        xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 5.75
        xlSheet.Columns("J:J").Select : myExcel.Selection.ColumnWidth = 4
        xlSheet.Columns("K:K").Select : myExcel.Selection.ColumnWidth = 45
        xlSheet.Columns("L:L").Select : myExcel.Selection.ColumnWidth = 12.75
        xlSheet.Columns("M:M").Select : myExcel.Selection.ColumnWidth = 40
        xlSheet.Columns("N:N").Select : myExcel.Selection.ColumnWidth = 6.6
        xlSheet.Columns("O:O").Select : myExcel.Selection.ColumnWidth = 6
        xlSheet.Columns("P:P").Select : myExcel.Selection.ColumnWidth = 30
        xlSheet.Range("A3:P3").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
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent1 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0 : End With
        xlSheet.Rows("4:4").Select : myExcel.ActiveWindow.FreezePanes = True
        xlSheet.Range("A1:P1").Select() : myExcel.Selection.Merge : With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlTop : End With
        xlSheet.Range("A2:P2").Select() : myExcel.Selection.Merge : With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlTop : End With
    End Sub
    Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        位置1 = "A4:P" & N1 : xlSheet.Range(位置1).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 CC(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        位置1 = "A" & N3 & ":A" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
        位置1 = "B" & N3 & ":B" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
        位置1 = "C" & N3 & ":C" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
        位置1 = "D" & N3 & ":D" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
        位置1 = "E" & N3 & ":E" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
        位置1 = "F" & N3 & ":F" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
        位置1 = "H" & N3 & ":H" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
        位置1 = "I" & N3 & ":I" & N4 : xlSheet.Range(位置1).Select() : myExcel.Selection.Merge
    End Sub
    Private Sub GG(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
        Dim 位置1 As String
        Clipboard.SetDataObject(PictureBox1.Image)
        位置1 = "A" & N3 : xlSheet.Range(位置1).Select()
        myExcel.ActiveSheet.Pictures.Paste.Select
        myExcel.Selection.ShapeRange.Width = 8
    End Sub
End Class