Option Strict Off 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 列印_產品標籤介面 ReadOnly ds As New DataSet : Dim N1 As Integer : Dim N2 As Integer Dim xlApp As Application : Dim xlBook As Workbook : Dim xlSheet As Worksheet Private Sub Set_清單() DataGridView1.DataSource = Nothing : ds.Clear() : DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing : DataGridView1.ColumnHeadersHeight = 40 DataGridView1.AllowUserToAddRows = False : DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText : DataGridView1.MultiSelect = True SQL_列印_產品標籤清單() da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() End Sub Private Sub 列印_產品標籤介面_Load(sender As Object, e As EventArgs) Handles MyBase.Load FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False Set_清單() CheckBox1.Checked = True End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 列印用SQL = "" : PA = "" : Me.Close() 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 xlApp.Visible = True xlApp.Application.WindowState = xlMaximized For I As Integer = 0 To DataGridView1.Rows.Count - 1 xlSheet = NewMethod(xlBook) : xlBook.Activate() : xlSheet.Activate() xlApp.Sheets(1).Name = DataGridView1.Rows(I).Cells("料號").Value AA(xlApp, xlSheet) If PA = "維修申請單" Then TextBox1.Text = DataGridView1.Rows(I).Cells("出庫").Value : PA1 = "維修申請單-出庫" ElseIf PA = "銷售退貨單" Then TextBox1.Text = DataGridView1.Rows(I).Cells("出庫").Value * -1 : PA1 = "銷售退貨單-退庫" ElseIf PA = "銷售出貨單" Then TextBox1.Text = DataGridView1.Rows(I).Cells("出庫").Value : PA1 = "銷售出貨單-出庫" ElseIf PA = "採購訂貨單" Then TextBox1.Text = DataGridView1.Rows(I).Cells("入庫").Value : PA1 = "採購訂貨單-入庫" ElseIf PA = "採購退貨單" Then TextBox1.Text = DataGridView1.Rows(I).Cells("入庫").Value * -1 : PA1 = "採購退貨單-退庫" End If If CheckBox1.Checked = True Then N1 = 0 For II As Integer = 1 To Val(TextBox1.Text) xlSheet.Cells(1 + N1, 1) = MAOJI_ERP_SYS.ComboBox2.Text : xlSheet.Cells(2 + N1, 1) = PA1 xlSheet.Cells(3 + N1, 1) = "系統單號 : " : xlSheet.Cells(3 + N1, 2) = DataGridView1.Rows(I).Cells("單據編號").Value : xlSheet.Cells(3 + N1, 3) = "單據數量" xlSheet.Cells(4 + N1, 1) = "系統料號 : " : xlSheet.Cells(4 + N1, 2) = DataGridView1.Rows(I).Cells("料號").Value : xlSheet.Cells(4 + N1, 3) = TextBox1.Text xlSheet.Cells(5 + N1, 1) = "產品頁碼 : " : xlSheet.Cells(5 + N1, 2) = DataGridView1.Rows(I).Cells("頁碼").Value xlSheet.Cells(6 + N1, 1) = "產品名稱 : " : xlSheet.Cells(6 + N1, 2) = DataGridView1.Rows(I).Cells("品名").Value xlSheet.Cells(7 + N1, 1) = "產品件數 : 共 ( )" BB(xlApp, xlSheet) N1 += 8 Next Else xlSheet.Cells(1, 1) = MAOJI_ERP_SYS.ComboBox2.Text : xlSheet.Cells(2, 1) = PA1 xlSheet.Cells(3, 1) = "系統單號 : " : xlSheet.Cells(3, 2) = DataGridView1.Rows(I).Cells("項次").Value : xlSheet.Cells(6, 3) = "單據數量" xlSheet.Cells(4, 1) = "系統料號 : " : xlSheet.Cells(4, 2) = DataGridView1.Rows(I).Cells("項次").Value : xlSheet.Cells(4, 3) = TextBox1.Text xlSheet.Cells(5, 1) = "產品頁碼 : " : xlSheet.Cells(5, 2) = DataGridView1.Rows(I).Cells("項次").Value xlSheet.Cells(6, 1) = "產品名稱 : " : xlSheet.Cells(6, 2) = DataGridView1.Rows(I).Cells("項次").Value xlSheet.Cells(7, 1) = "產品件數 : 共 ( )" BB(xlApp, xlSheet) End If CC(xlApp, xlSheet) Next N2 = DataGridView1.Rows.Count + 1 : xlApp.Sheets(N2).Delete xlSheet.PageSetup.PrintArea = "" xlApp.Cells.Select() xlSheet.Range("B1").Select() xlApp.Application.WindowState = xlMinimized xlSheet.Cells.Select() MsgBox("列印完成。") 列印用SQL = "" : PA = "" : 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 = 12 : .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 = 10 xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 30 xlSheet.Columns("C:C").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.ActiveWindow.DisplayGridlines = False End Sub Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet) xlSheet.Range("A" & 1 + N1 & ":C" & 1 + N1).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("A" & 2 + N1 & ":C" & 2 + N1).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("A" & 1 + N1 & ":C" & 7 + N1).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 = xlMedium : End With With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone xlSheet.Rows(1 + N1 & ":" & 7 + N1).Select : myExcel.Selection.RowHeight = 22 xlSheet.Rows(8 + N1 & ":" & 8 + N1).Select : myExcel.Selection.RowHeight = 5 End Sub Private Sub CC(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.ActiveSheet.PageSetup.PrintArea = "" myExcel.Application.PrintCommunication = False With myExcel.ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = myExcel.Application.InchesToPoints(0.118110236220472) .RightMargin = myExcel.Application.InchesToPoints(0.118110236220472) .TopMargin = myExcel.Application.InchesToPoints(0.196850393700787) .BottomMargin = myExcel.Application.InchesToPoints(0.196850393700787) .HeaderMargin = myExcel.Application.InchesToPoints(0.31496062992126) .FooterMargin = myExcel.Application.InchesToPoints(0.31496062992126) .PrintHeadings = False .PrintGridlines = False '.PrintQuality = 203 .CenterHorizontally = True .CenterVertically = True .Draft = False '.PaperSize = 260 .FirstPageNumber = xlAutomatic .BlackAndWhite = False .Zoom = 90 .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