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 Public Class 列印清單 ReadOnly ds As New DataSet Private Sub Set_DGV1載入前設定() DataGridView1.DataSource = Nothing : ds.Clear() DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing DataGridView1.ColumnHeadersHeight = 25 DataGridView1.AllowUserToAddRows = False ConnOpen() End Sub Private Sub Set_DGV1載入後設定() CmdSet_For_DGV() : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() End Sub Private Sub Set_庫存清單() Set_DGV1載入前設定() : SQL1 = 列印用SQL1 : Set_DGV1載入後設定() End Sub Private Sub Set_格式設定() DataGridView1.Visible = False End Sub Private Sub 列印清單_Load(sender As Object, e As EventArgs) Handles MyBase.Load FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False Set_庫存清單() : Set_格式設定() End Sub Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove timeNow = 0 GCM_ERP_SYS.Timer1.Enabled = False GCM_ERP_SYS.Timer1.Enabled = True End Sub Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress timeNow = 0 GCM_ERP_SYS.Timer1.Enabled = False GCM_ERP_SYS.Timer1.Enabled = True End Sub Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Office.Interop.Excel.Workbook Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet 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 If 列印(0) = True Then xlSheet.Cells(3, 1) = "零件編號" xlSheet.Cells(3, 2) = "進貨日期" xlSheet.Cells(3, 3) = "品牌" xlSheet.Cells(3, 4) = "品名" xlSheet.Cells(3, 5) = "型號及備註" xlSheet.Cells(3, 6) = "進貨單價" xlSheet.Cells(3, 7) = "庫存數量" xlSheet.Cells(3, 8) = "安全庫存" xlSheet.Cells(3, 9) = "庫存成本" xlSheet.Cells(3, 10) = "台幣庫存成本" xlSheet.Cells(3, 11) = "匯率" xlSheet.Cells(3, 12) = "供應商" xlSheet.Cells(1, 1) = "工巧明設備庫存清單" : xlSheet.Cells(1, 7) = "庫存總數" : xlSheet.Cells(1, 9) = "庫存總金额" : xlSheet.Cells(1, 10) = "台幣庫存總成本" xlSheet.Cells(2, 7) = "=SUBTOTAL(9,G4:G$99999)" : xlSheet.Cells(2, 9) = "=SUBTOTAL(9,I4:I$99999)" : xlSheet.Cells(2, 10) = "=SUBTOTAL(9,J4:J$99999)" For i As Integer = 0 To DataGridView1.Rows.Count - 1 xlSheet.Cells(i + 4, 1) = DataGridView1.Rows(i).Cells("零件編號").Value xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("進貨日期").Value xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("品牌").Value xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("品名").Value xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("型號及備註").Value xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("進貨單價").Value xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("庫存數量").Value xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("安全庫存").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("匯率").Value xlSheet.Cells(i + 4, 12) = DataGridView1.Rows(i).Cells("供應商").Value With xlApp.Selection .Font.Bold = True End With Next i ElseIf 列印(1) = True Then xlSheet.Cells(3, 1) = "零件編號" xlSheet.Cells(3, 2) = "結單日期" xlSheet.Cells(3, 3) = "品名" xlSheet.Cells(3, 4) = "型號及備註" xlSheet.Cells(3, 5) = "出貨數量" xlSheet.Cells(3, 6) = "進貨成本" xlSheet.Cells(3, 7) = "出貨金額" xlSheet.Cells(3, 8) = "利潤" xlSheet.Cells(3, 9) = "台幣利潤" xlSheet.Cells(1, 1) = "工巧明設備出庫紀錄清單" xlSheet.Cells(1, 5) = "出貨總數量" : xlSheet.Cells(1, 6) = "總成本" : xlSheet.Cells(1, 7) = "總金額" : xlSheet.Cells(1, 8) = "總利潤" : xlSheet.Cells(1, 9) = "台幣總利潤" xlSheet.Cells(2, 5) = "=SUBTOTAL(9,E4:E$99999)" : xlSheet.Cells(2, 6) = "=SUBTOTAL(9,F4:F$99999)" : xlSheet.Cells(2, 7) = "=SUBTOTAL(9,G4:G$99999)" xlSheet.Cells(2, 8) = "=SUBTOTAL(9,H4:H$99999)" : xlSheet.Cells(2, 9) = "=SUBTOTAL(9,I4:I$99999)" For i As Integer = 0 To DataGridView1.Rows.Count - 1 xlSheet.Cells(i + 4, 1) = DataGridView1.Rows(i).Cells("零件編號").Value xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("結單日期").Value xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("品名").Value xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("型號及備註").Value xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("出貨數量").Value xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("進貨成本").Value xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("出貨金額").Value xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("利潤").Value xlSheet.Cells(i + 4, 9) = DataGridView1.Rows(i).Cells("台幣利潤").Value With xlApp.Selection .Font.Bold = True End With Next i End If BB(xlApp, xlSheet) xlSheet.PageSetup.PrintArea = "" xlApp.Cells.Select() xlSheet.Range("B1").Select() xlApp.Application.WindowState = xlMinimized MsgBox("列印完成") 列印(0) = False : 列印(1) = False : 列印用SQL1 = "" 列印用SQL1 = "" : Me.Close() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 列印(0) = False : 列印(1) = False : 列印用SQL1 = "" 列印用SQL1 = "" : 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 BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet) Dim 位置1 As String If 列印(0) = True Then 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("F:H").Select : myExcel.Selection.NumberFormatLocal = "#,##0_ ;[紅色]-#,##0 " xlSheet.Columns("I:J").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00_ ;[紅色]-#,##0.00 " xlSheet.Columns("K:K").Select : myExcel.Selection.NumberFormatLocal = "0.0000_ ;[紅色]-0.0000 " xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 9 xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 9.5 xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 9 xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 30 xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 40 xlSheet.Columns("F:L").Select : xlSheet.Columns("F:L").EntireColumn.AutoFit xlSheet.Rows("1:2").Select : myExcel.Selection.RowHeight = 15 xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 30 With myExcel.Selection .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With xlSheet.Range("A3:L3").Select() : myExcel.Selection.AutoFilter xlSheet.Rows("4:4").Select : myExcel.ActiveWindow.FreezePanes = True xlSheet.Range("A1:F2,H1:H2,K1:L2").Select() xlSheet.Range("K1").Activate() 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("A1:F2").Select() With myExcel.Selection.Font .Name = "微軟正黑體" : .Size = 14 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone End With Dim M As Integer = 4 For M = 4 To DataGridView1.Rows.Count + 2 M += 1 Next M 位置1 = "A1:L" & M 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 xlSheet.Range("A1:F2").Select() : myExcel.ActiveWindow.DisplayGridlines = False xlSheet.Columns("A:B").Select : xlSheet.Range("A3").Activate() With myExcel.Selection .HorizontalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext End With xlSheet.Range("A3:L3").Select() With myExcel.Selection .HorizontalAlignment = xlCenter : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With xlSheet.Range("A3:L3").Select() With myExcel.Selection.Interior .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent4 : .TintAndShade = 0.599993896298105 : .PatternTintAndShade = 0 End With 位置1 = "C4:E" & M xlSheet.Range(位置1).Select() With myExcel.Selection .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With 位置1 = "F4:F" & M xlSheet.Range(位置1).Select() With myExcel.Selection .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With ElseIf 列印(1) = True Then 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:I").Select : xlSheet.Columns("A:I").EntireColumn.AutoFit xlSheet.Rows("1:2").Select : myExcel.Selection.RowHeight = 15 xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 30 xlSheet.Range("A3:I3").Select() With myExcel.Selection .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With myExcel.Selection.AutoFilter xlSheet.Columns("A:B").Select xlSheet.Range("B1").Activate() : myExcel.Selection.ColumnWidth = 10 Dim M As Integer = 4 For M = 4 To DataGridView1.Rows.Count + 2 M += 1 Next M 位置1 = "A1:I" & M 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 xlSheet.Rows("4:4").Select myExcel.ActiveWindow.FreezePanes = True : myExcel.ActiveWindow.DisplayGridlines = False xlSheet.Columns("C:C").Select : myExcel.Selection.NumberFormatLocal = "#,##0_ ;[紅色]-#,##0 " xlSheet.Columns("F:I").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00_ ;[紅色]-#,##0.00 " xlSheet.Columns("F:I").Select : myExcel.Selection.ColumnWidth = 12 xlSheet.Range("A3:I3").Select() With myExcel.Selection.Interior .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent1 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0 End With xlSheet.Range("A1:D2").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 With myExcel.Selection.Font .Name = "微軟正黑體" : .Size = 14 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone End With xlSheet.Range("A3:I3").Select() With myExcel.Selection .HorizontalAlignment = xlCenter : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With xlSheet.Range("E1:I1").Select() With myExcel.Selection .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With End If End Sub End Class