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 列印_供應商資料表 Private ReadOnly ds As New DataSet Dim N2 As Integer Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Office.Interop.Excel.Workbook Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet Private Sub Set_清單() DataGridView1.DataSource = Nothing : ds.Clear() DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing DataGridView1.ColumnHeadersHeight = 25 DataGridView1.AllowUserToAddRows = False ConnOpen() SQL1 = 列印用SQL CmdSet_For_DGV() da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() 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 Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click 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 xlSheet.Cells(1, 1) = "巨益貿易-供應商資料表" : xlSheet.Cells(2, 1) = "供應商編碼" : xlSheet.Cells(2, 2) = "供應商簡稱" : xlSheet.Cells(2, 3) = "公司名" : xlSheet.Cells(2, 4) = "公司地址" xlSheet.Cells(2, 5) = "公司電話1" : xlSheet.Cells(2, 6) = "公司電話2" : xlSheet.Cells(2, 7) = "公司電話3" : xlSheet.Cells(2, 8) = "公司傳真1" : xlSheet.Cells(2, 9) = "公司傳真2" xlSheet.Cells(2, 10) = "廠商類別" : xlSheet.Cells(2, 11) = "聯繫人1" : xlSheet.Cells(2, 12) = "聯繫人2" : xlSheet.Cells(2, 13) = "聯繫人3" : xlSheet.Cells(2, 14) = "聯繫人1職稱" xlSheet.Cells(2, 15) = "聯繫人2職稱" : xlSheet.Cells(2, 16) = "聯繫人3職稱" : xlSheet.Cells(2, 17) = "聯繫人1電話" : xlSheet.Cells(2, 18) = "聯繫人2電話" : xlSheet.Cells(2, 19) = "聯繫人3電話" xlSheet.Cells(2, 20) = "聯繫人1郵箱" : xlSheet.Cells(2, 21) = "聯繫人2郵箱" : xlSheet.Cells(2, 22) = "聯繫人3郵箱" For i As Integer = 0 To DataGridView1.Rows.Count - 1 xlSheet.Cells(i + 3, 1) = DataGridView1.Rows(i).Cells("供應商編碼").Value : xlSheet.Cells(i + 3, 2) = DataGridView1.Rows(i).Cells("廠商").Value xlSheet.Cells(i + 3, 3) = DataGridView1.Rows(i).Cells("公司名").Value : xlSheet.Cells(i + 3, 4) = DataGridView1.Rows(i).Cells("公司地址").Value xlSheet.Cells(i + 3, 5) = DataGridView1.Rows(i).Cells("公司電話1").Value : xlSheet.Cells(i + 3, 6) = DataGridView1.Rows(i).Cells("公司電話2").Value xlSheet.Cells(i + 3, 7) = DataGridView1.Rows(i).Cells("公司電話3").Value : xlSheet.Cells(i + 3, 8) = DataGridView1.Rows(i).Cells("公司傳真1").Value xlSheet.Cells(i + 3, 9) = DataGridView1.Rows(i).Cells("公司傳真2").Value : xlSheet.Cells(i + 3, 10) = DataGridView1.Rows(i).Cells("公司傳真3").Value xlSheet.Cells(i + 3, 11) = DataGridView1.Rows(i).Cells("聯繫人1").Value : xlSheet.Cells(i + 3, 12) = DataGridView1.Rows(i).Cells("聯繫人2").Value xlSheet.Cells(i + 3, 13) = DataGridView1.Rows(i).Cells("聯繫人3").Value : xlSheet.Cells(i + 3, 14) = DataGridView1.Rows(i).Cells("聯繫人1職稱").Value xlSheet.Cells(i + 3, 15) = DataGridView1.Rows(i).Cells("聯繫人2職稱").Value : xlSheet.Cells(i + 3, 16) = DataGridView1.Rows(i).Cells("聯繫人3職稱").Value xlSheet.Cells(i + 3, 17) = DataGridView1.Rows(i).Cells("聯繫人1電話").Value : xlSheet.Cells(i + 3, 18) = DataGridView1.Rows(i).Cells("聯繫人2電話").Value xlSheet.Cells(i + 3, 19) = DataGridView1.Rows(i).Cells("聯繫人3電話").Value : xlSheet.Cells(i + 3, 20) = DataGridView1.Rows(i).Cells("聯繫人1郵箱").Value xlSheet.Cells(i + 3, 21) = DataGridView1.Rows(i).Cells("聯繫人2郵箱").Value : xlSheet.Cells(i + 3, 22) = DataGridView1.Rows(i).Cells("聯繫人3郵箱").Value With xlApp.Selection .Font.Bold = True End With N2 = i + 3 Next i 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 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 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.Rows("1:1").Select : myExcel.Selection.RowHeight = 30 : xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 20 xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 10 : xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 17 xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 30 : xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 60 xlSheet.Columns("E:S").Select : myExcel.Selection.ColumnWidth = 12 : xlSheet.Columns("T:V").Select : myExcel.Selection.ColumnWidth = 30 xlSheet.Range("D3").Select() : myExcel.ActiveWindow.FreezePanes = True xlSheet.Range("A1:C1").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 xlSheet.Range("A1:C1").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 位置1 = "A2:V" & N2 : 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("A2:V2").Select() With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent2 : .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 : End With xlSheet.Range("P12").Select() myExcel.ActiveWindow.DisplayGridlines = False End Sub End Class