Option Strict Off Imports System.Text.RegularExpressions 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 樣品標籤批量列印 Private ReadOnly ds, ds1, ds2, ds4, ds5, ds6 As New DataSet Dim DDFF, BBNN11, AAWW, HHGG, X1, X2 As String Private ReadOnly RRTT As New Regex("[\u4e00-\u9fa5]") Dim N1, N2, N3, BBNN1 As Integer Dim 已超出 As Boolean Dim xlApp As Application : Dim xlBook As Workbook : Dim xlSheet As Worksheet Private Sub Set_DGV3載入前設定() 主表單_dgv.DataSource = Nothing : ds2.Clear() 主表單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing 主表單_dgv.ColumnHeadersHeight = 25 主表單_dgv.AllowUserToAddRows = False End Sub Private Sub Set_DGV3載入後設定() da.Fill(ds2) : 主表單_dgv.DataSource = ds2.Tables(0) : conn.Close() Set_grid2() End Sub Private Sub Set_清單2() Set_DGV3載入前設定() SQL_主表單() Set_DGV3載入後設定() End Sub Private Sub Set_grid2() 主表單_dgv.Columns(0).Width = 60 : 主表單_dgv.Columns(1).Width = 150 : 主表單_dgv.Columns(2).Width = 150 : 主表單_dgv.Columns(3).Width = 150 : 主表單_dgv.Columns(4).Width = 150 主表單_dgv.Columns(5).Width = 150 : 主表單_dgv.Columns(6).Width = 150 : 主表單_dgv.Columns(7).Width = 150 : 主表單_dgv.Columns(8).Width = 150 : 主表單_dgv.Columns(9).Width = 303 主表單_dgv.Columns(10).Width = 302 : 主表單_dgv.Columns(11).Visible = False 主表單_dgv.Columns(7).DefaultCellStyle.Format = "#,##0.0" 主表單_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight 主表單_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight 主表單_dgv.EditMode = DataGridViewEditMode.EditOnEnter For i As Integer = 0 To 主表單_dgv.Rows.Count - 1 : 主表單_dgv.Rows(i).Cells("選擇").Value = False : Next i For i As Integer = 0 To 主表單_dgv.Rows.Count - 1 資料數 = 主表單_dgv.Rows.Count : MyModule1.進度條() PA = 主表單_dgv.Rows(i).Cells("流水號").Value SQL_一筆材料名稱() If (dr.Read()) Then : DDFF = dr("材料名稱") : Else : DDFF = "" : End If : conn.Close() '-------------------------------------------------------------------------------------------- BBNN1 = DDFF.Length.ToString() For II As Integer = 0 To BBNN1 - 1 If II = 0 Then AAWW = Strings.Left(DDFF, 1) If RRTT.IsMatch(AAWW) Then : Else : BBNN11 = Strings.Left(DDFF, 1) : End If Else AAWW = Strings.Mid(DDFF, II + 1, 1) : If RRTT.IsMatch(AAWW) Then : Else : HHGG = BBNN11 : BBNN11 = HHGG & AAWW : End If End If Next : 主表單_dgv.Rows(i).Cells("面料").Value = BBNN11 '-------------------------------------------------------------------------------------------- SQL_一筆材料名稱1() If (dr.Read()) Then : DDFF = dr("材料名稱") : Else : DDFF = "" : End If : conn.Close() '-------------------------------------------------------------------------------------------- BBNN1 = DDFF.Length.ToString() For II As Integer = 0 To BBNN1 - 1 If II = 0 Then AAWW = Strings.Left(DDFF, 1) If RRTT.IsMatch(AAWW) Then : Else : BBNN11 = Strings.Left(DDFF, 1) : End If Else AAWW = Strings.Mid(DDFF, II + 1, 1) : If RRTT.IsMatch(AAWW) Then : Else : HHGG = BBNN11 : BBNN11 = HHGG & AAWW : End If End If Next : 主表單_dgv.Rows(i).Cells("大底").Value = BBNN11 '-------------------------------------------------------------------------------------------- Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 End Sub Private Sub ComboBox1下拉表單資料載入() SQL_季節清單() 季節_cb.Items.Clear() : While (dr.Read()) : 季節_cb.Items.Add(dr("SEASON")) : End While : conn.Close() End Sub Private Sub ComboBox2下拉表單資料載入() SQL_客戶簡稱() 季節1_cb.Items.Clear() : While (dr.Read()) : 季節1_cb.Items.Add(dr("客戶簡稱")) : End While : conn.Close() End Sub Private Sub 樣品標籤批量列印_Load(sender As Object, e As EventArgs) Handles MyBase.Load WINPROFIT_ERP_SYS.WindowState = 2 : Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() End Sub Private Sub DataGridView3_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 主表單_dgv.RowPostPaint Dim linePen As New Pen(Color.Blue, 2) If e.RowIndex = 主表單_dgv.Rows.Count - 1 Then Exit Sub Else If 主表單_dgv(2, e.RowIndex).Value.ToString <> 主表單_dgv(2, e.RowIndex + 1).Value.ToString Then Dim startX As Integer = IIf(主表單_dgv.RowHeadersVisible, 主表單_dgv.RowHeadersWidth, 0) Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1 Dim endX As Integer = startX + 主表單_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 主表單_dgv.HorizontalScrollingOffset e.Graphics.DrawLine(linePen, startX, startY, endX, startY) Exit Sub End If End If End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 季節_cb.SelectedIndexChanged If 季節_cb.Text <> "" And 季節1_cb.Text <> "" Then : Set_清單2() : End If End Sub Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 季節1_cb.SelectedIndexChanged If 季節_cb.Text <> "" And 季節1_cb.Text <> "" Then : Set_清單2() : End If End Sub Private Sub Button13_Click(sender As Object, e As EventArgs) Handles 全部選擇_bt.Click For i As Integer = 0 To 主表單_dgv.Rows.Count - 1 : 主表單_dgv.Rows(i).Cells("選擇").Value = True : Next i End Sub Private Sub Button12_Click(sender As Object, e As EventArgs) Handles 全部取消_bt.Click For i As Integer = 0 To 主表單_dgv.Rows.Count - 1 : 主表單_dgv.Rows(i).Cells("選擇").Value = False : Next i End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 列印_bt.Click xlApp = CType(CreateObject("Excel.Application"), Application) xlBook = xlApp.Workbooks.Add xlApp.DisplayAlerts = True xlBook.Activate() xlSheet = NewMethod(xlBook) xlSheet.Activate() xlApp.Visible = True xlApp.Application.WindowState = xlMaximized N1 = 0 : N2 = 0 : N3 = 0 For i As Integer = 0 To 主表單_dgv.Rows.Count - 1 For ii As Integer = 1 To 主表單_dgv.Rows(i).Cells("數量").Value * 2 If 主表單_dgv.Rows(i).Cells("選擇").Value = True Then If 主表單_dgv.Rows(i).Cells("數量").Value <> 0 Then xlSheet.Cells(1 + N1, 1 + N3) = " ME TOO" xlSheet.Cells(2 + N1, 1 + N3) = " " & 樣品進度表分類5 xlSheet.Cells(3 + N1, 1 + N3) = "Season" : xlSheet.Cells(3 + N1, 2 + N3) = 季節_cb.Text xlSheet.Cells(4 + N1, 1 + N3) = "Customer:" : xlSheet.Cells(4 + N1, 2 + N3) = 季節1_cb.Text xlSheet.Cells(5 + N1, 1 + N3) = "Styie#/Name:" xlSheet.Cells(5 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("型體號碼").Value : xlSheet.Cells(6 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("型體名稱").Value xlSheet.Cells(7 + N1, 1 + N3) = "Last/Const:" : xlSheet.Cells(7 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("模具編號").Value xlSheet.Cells(8 + N1, 1 + N3) = "Upper Mtrl:" : xlSheet.Cells(8 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("面料").Value xlSheet.Cells(9 + N1, 1 + N3) = "Color:" : xlSheet.Cells(9 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("顏色").Value xlSheet.Cells(10 + N1, 1 + N3) = "Outsole:" : xlSheet.Cells(10 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("大底").Value xlSheet.Cells(11 + N1, 1 + N3) = "Size:" : xlSheet.Cells(11 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("SIZE").Value xlSheet.Cells(12 + N1, 1 + N3) = "Date:" : xlSheet.Cells(12 + N1, 2 + N3) = 主表單_dgv.Rows(i).Cells("ETD").Value AA(xlApp, xlSheet) N1 += 12 : N2 += 1 If N2 = 100 Or N2 = 200 Or N2 = 300 Or N2 = 400 Or N2 = 500 Or N2 = 600 Or N2 = 700 Or N2 = 800 Or N2 = 900 Or N2 = 1000 Or N2 = 1100 Or N2 = 1200 Or N2 = 1300 Or N2 = 1400 Or N2 = 1500 Or N2 = 1600 Or N2 = 1700 Or N2 = 1800 Or N2 = 1900 Or N2 = 2000 Or N2 = 2100 Or N2 = 2200 Or N2 = 2300 Or N2 = 2400 Or N2 = 2500 Or N2 = 2600 Or N2 = 2700 Or N2 = 2800 Or N2 = 2900 Or N2 = 3000 Or N2 = 3100 Or N2 = 3200 Then N3 += 2 : N1 = 0 End If End If End If Next Next i BB(xlApp, xlSheet) xlSheet.PageSetup.PrintArea = "" xlApp.Cells.Select() xlSheet.Range("B1").Select() xlApp.Application.WindowState = xlMinimized MsgBox("列印完成") End Sub Private Shared Function NewMethod(xlBook As Workbook) As Worksheet Return CType(xlBook.Worksheets.Add, Worksheet) End Function Private Sub 分行() If N2 = 0 Then : X1 = "A" : X2 = "B" : ElseIf N2 = 100 Then : X1 = "C" : X2 = "D" : ElseIf N2 = 200 Then : X1 = "E" : X2 = "F" : ElseIf N2 = 300 Then : X1 = "G" : X2 = "H" ElseIf N2 = 400 Then : X1 = "I" : X2 = "J" : ElseIf N2 = 500 Then : X1 = "K" : X2 = "L" : ElseIf N2 = 600 Then : X1 = "M" : X2 = "N" : ElseIf N2 = 700 Then : X1 = "O" : X2 = "P" ElseIf N2 = 800 Then : X1 = "Q" : X2 = "R" : ElseIf N2 = 900 Then : X1 = "S" : X2 = "T" : ElseIf N2 = 1000 Then : X1 = "U" : X2 = "V" : ElseIf N2 = 1100 Then : X1 = "W" : X2 = "X" ElseIf N2 = 1200 Then : X1 = "Y" : X2 = "Z" : ElseIf N2 = 1300 Then : X1 = "AA" : X2 = "AB" : ElseIf N2 = 1400 Then : X1 = "AC" : X2 = "AD" : ElseIf N2 = 1500 Then : X1 = "AE" : X2 = "AF" ElseIf N2 = 1600 Then : X1 = "AG" : X2 = "AH" : ElseIf N2 = 1700 Then : X1 = "AI" : X2 = "AJ" : ElseIf N2 = 1800 Then : X1 = "AK" : X2 = "AL" : ElseIf N2 = 1900 Then : X1 = "AM" : X2 = "AN" ElseIf N2 = 2000 Then : X1 = "AO" : X2 = "AP" : ElseIf N2 = 2100 Then : X1 = "AQ" : X2 = "AR" : ElseIf N2 = 2200 Then : X1 = "AS" : X2 = "AT" : ElseIf N2 = 2300 Then : X1 = "AU" : X2 = "AV" ElseIf N2 = 2400 Then : X1 = "AW" : X2 = "AX" : ElseIf N2 = 2500 Then : X1 = "AY" : X2 = "AZ" : ElseIf N2 = 2600 Then : X1 = "BA" : X2 = "BB" : ElseIf N2 = 2700 Then : X1 = "BC" : X2 = "BD" ElseIf N2 = 2800 Then : X1 = "BE" : X2 = "BF" : ElseIf N2 = 2900 Then : X1 = "BG" : X2 = "BH" : ElseIf N2 = 3000 Then : X1 = "BI" : X2 = "BJ" : ElseIf N2 = 3100 Then : X1 = "BK" : X2 = "BL" ElseIf N2 = 3200 Then : X1 = "BM" : X2 = "BN" : End If End Sub Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet) 分行() xlSheet.Range(X1 & N1 + 1 & ":" & X2 & N1 + 12).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 xlSheet.Range(X1 & N1 + 1 & ":" & X2 & N1 + 1).Select() 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 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(X2 & N1 + 3 & ":" & X2 & N1 + 12).Select() With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With xlSheet.Range(X2 & N1 + 3 & ":" & X2 & N1 + 12).Select() With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With xlSheet.Range(X1 & N1 + 1 & ":" & X2 & N1 + 2).Select() With myExcel.Selection.Font : .Name = "微軟正黑體" : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With xlSheet.Rows(N1 + 1 & ":" & N1 + 1).Select : myExcel.Selection.RowHeight = 60 xlSheet.Rows(N1 + 2 & ":" & N1 + 2).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 3 & ":" & N1 + 3).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 4 & ":" & N1 + 4).Select : myExcel.Selection.RowHeight = 25 xlSheet.Rows(N1 + 5 & ":" & N1 + 5).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 6 & ":" & N1 + 6).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 7 & ":" & N1 + 7).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 8 & ":" & N1 + 8).Select : myExcel.Selection.RowHeight = 35 xlSheet.Rows(N1 + 9 & ":" & N1 + 9).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 10 & ":" & N1 + 10).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 11 & ":" & N1 + 11).Select : myExcel.Selection.RowHeight = 20 xlSheet.Rows(N1 + 12 & ":" & N1 + 12).Select : myExcel.Selection.RowHeight = 20 xlSheet.Cells.Select() : myExcel.Selection.Font.Bold = True xlSheet.Columns(X1 & ":" & X1).Select : myExcel.Selection.ColumnWidth = 10 xlSheet.Columns(X2 & ":" & X2).Select : myExcel.Selection.ColumnWidth = 20 End Sub Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet) myExcel.Application.PrintCommunication = False With myExcel.ActiveSheet.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" End With myExcel.Application.PrintCommunication = True myExcel.Application.PrintCommunication = False With myExcel.ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = myExcel.Application.InchesToPoints(0) .RightMargin = myExcel.Application.InchesToPoints(0) .TopMargin = myExcel.Application.InchesToPoints(0.0393700787401575) .BottomMargin = myExcel.Application.InchesToPoints(0.0393700787401575) .HeaderMargin = myExcel.Application.InchesToPoints(0) .FooterMargin = myExcel.Application.InchesToPoints(0) .PrintHeadings = False .PrintGridlines = False .CenterHorizontally = True .CenterVertically = True .Draft = False .FirstPageNumber = xlAutomatic .BlackAndWhite = False .Zoom = 55 .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 Private Sub 主表單_dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 主表單_dgv.MouseUp If 主表單_dgv.SelectedCells.Count = 1 Then 已超出 = False End If If 主表單_dgv.SelectedCells.Count > 0 Then '設定滑鼠點擊DGV時候的範圍,超出範圍時有可能不是數字所以不計算 If 主表單_dgv.SelectedCells(0).ColumnIndex = 7 Then If 已超出 = False Then Dim x As Double = 0 For i As Integer = 0 To 主表單_dgv.SelectedCells.Count - 1 '主表單_dgv.SelectedCells.Item(i).ColumnIndex + 8 是因為該DGV顯示項與計算像剛好差8格,使用時要注意可直接計算的不需要加數字。 x += Val(主表單_dgv.Rows(主表單_dgv.SelectedCells.Item(i).RowIndex).Cells(主表單_dgv.SelectedCells.Item(i).ColumnIndex).Value) Next i '將計算好的資料放置到指定的控件中 WINPROFIT_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(x, "#,##0.0") Else WINPROFIT_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0.0") End If Else 已超出 = True WINPROFIT_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0.0") End If End If End Sub End Class