Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

列印_供應商資料表.vb 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. Option Strict Off
  2. Imports System.Data.SqlClient
  3. Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
  4. Imports Microsoft.Office.Interop.Excel.Constants
  5. Imports Microsoft.Office.Interop.Excel.XlBordersIndex
  6. Imports Microsoft.Office.Interop.Excel.XlLineStyle
  7. Imports Microsoft.Office.Interop.Excel.XlBorderWeight
  8. Imports Microsoft.Office.Interop.Excel.XlThemeFont
  9. Imports Microsoft.Office.Interop.Excel.XlThemeColor
  10. Imports Microsoft.Office.Interop.Excel.XlWindowState
  11. Public Class 列印_供應商資料表
  12. Dim conn As New SqlConnection
  13. Dim da As New SqlDataAdapter
  14. Dim cmd As New SqlCommand
  15. Dim ds As New DataSet
  16. Dim dr As SqlDataReader
  17. Dim N1 As Integer : Dim N2 As Integer
  18. Dim xlApp As Microsoft.Office.Interop.Excel.Application
  19. Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
  20. Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
  21. Private Sub Set_DGV1載入前設定()
  22. DataGridView1.DataSource = Nothing : ds.Clear()
  23. DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  24. DataGridView1.ColumnHeadersHeight = 25
  25. DataGridView1.AllowUserToAddRows = False
  26. If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
  27. End Sub
  28. Private Sub Set_DGV1載入後設定()
  29. cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
  30. End Sub
  31. Private Sub Set_清單()
  32. Set_DGV1載入前設定() : SQL1 = 列印用SQL : Set_DGV1載入後設定()
  33. End Sub
  34. Private Sub Set_格式設定()
  35. DataGridView1.Visible = False
  36. End Sub
  37. Private Sub 列印_供應商資料表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  38. FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
  39. Set_清單() : Set_格式設定()
  40. End Sub
  41. Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
  42. xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
  43. xlBook = xlApp.Workbooks.Add
  44. xlApp.DisplayAlerts = True
  45. xlBook.Activate()
  46. xlSheet = NewMethod(xlBook)
  47. xlSheet.Activate()
  48. xlApp.Visible = True
  49. xlApp.Application.WindowState = xlMaximized
  50. xlSheet.Cells(1, 1) = "巨益貿易-供應商資料表" : xlSheet.Cells(2, 1) = "供應商編碼" : xlSheet.Cells(2, 2) = "供應商簡稱" : xlSheet.Cells(2, 3) = "公司名" : xlSheet.Cells(2, 4) = "公司地址"
  51. 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"
  52. xlSheet.Cells(2, 10) = "廠商類別" : xlSheet.Cells(2, 11) = "聯繫人1" : xlSheet.Cells(2, 12) = "聯繫人2" : xlSheet.Cells(2, 13) = "聯繫人3" : xlSheet.Cells(2, 14) = "聯繫人1職稱"
  53. 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電話"
  54. xlSheet.Cells(2, 20) = "聯繫人1郵箱" : xlSheet.Cells(2, 21) = "聯繫人2郵箱" : xlSheet.Cells(2, 22) = "聯繫人3郵箱"
  55. For i As Integer = 0 To DataGridView1.Rows.Count - 1
  56. xlSheet.Cells(i + 3, 1) = DataGridView1.Rows(i).Cells("供應商編碼").Value : xlSheet.Cells(i + 3, 2) = DataGridView1.Rows(i).Cells("廠商").Value
  57. xlSheet.Cells(i + 3, 3) = DataGridView1.Rows(i).Cells("公司名").Value : xlSheet.Cells(i + 3, 4) = DataGridView1.Rows(i).Cells("公司地址").Value
  58. xlSheet.Cells(i + 3, 5) = DataGridView1.Rows(i).Cells("公司電話1").Value : xlSheet.Cells(i + 3, 6) = DataGridView1.Rows(i).Cells("公司電話2").Value
  59. xlSheet.Cells(i + 3, 7) = DataGridView1.Rows(i).Cells("公司電話3").Value : xlSheet.Cells(i + 3, 8) = DataGridView1.Rows(i).Cells("公司傳真1").Value
  60. xlSheet.Cells(i + 3, 9) = DataGridView1.Rows(i).Cells("公司傳真2").Value : xlSheet.Cells(i + 3, 10) = DataGridView1.Rows(i).Cells("公司傳真3").Value
  61. xlSheet.Cells(i + 3, 11) = DataGridView1.Rows(i).Cells("聯繫人1").Value : xlSheet.Cells(i + 3, 12) = DataGridView1.Rows(i).Cells("聯繫人2").Value
  62. xlSheet.Cells(i + 3, 13) = DataGridView1.Rows(i).Cells("聯繫人3").Value : xlSheet.Cells(i + 3, 14) = DataGridView1.Rows(i).Cells("聯繫人1職稱").Value
  63. xlSheet.Cells(i + 3, 15) = DataGridView1.Rows(i).Cells("聯繫人2職稱").Value : xlSheet.Cells(i + 3, 16) = DataGridView1.Rows(i).Cells("聯繫人3職稱").Value
  64. xlSheet.Cells(i + 3, 17) = DataGridView1.Rows(i).Cells("聯繫人1電話").Value : xlSheet.Cells(i + 3, 18) = DataGridView1.Rows(i).Cells("聯繫人2電話").Value
  65. xlSheet.Cells(i + 3, 19) = DataGridView1.Rows(i).Cells("聯繫人3電話").Value : xlSheet.Cells(i + 3, 20) = DataGridView1.Rows(i).Cells("聯繫人1郵箱").Value
  66. xlSheet.Cells(i + 3, 21) = DataGridView1.Rows(i).Cells("聯繫人2郵箱").Value : xlSheet.Cells(i + 3, 22) = DataGridView1.Rows(i).Cells("聯繫人3郵箱").Value
  67. With xlApp.Selection
  68. .Font.Bold = True
  69. End With
  70. N2 = i + 3
  71. Next i
  72. BB(xlApp, xlSheet)
  73. xlSheet.PageSetup.PrintArea = ""
  74. xlApp.Cells.Select()
  75. xlSheet.Range("B1").Select()
  76. xlApp.Application.WindowState = xlMinimized
  77. MsgBox("列印完成")
  78. Me.Close()
  79. End Sub
  80. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  81. 列印用SQL = "" : Me.Close()
  82. End Sub
  83. Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
  84. Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
  85. End Function
  86. Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
  87. Dim 位置1 As String
  88. xlSheet.Cells.Select()
  89. With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  90. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
  91. xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 30 : xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 20
  92. xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 10 : xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 17
  93. xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 30 : xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 60
  94. xlSheet.Columns("E:S").Select : myExcel.Selection.ColumnWidth = 12 : xlSheet.Columns("T:V").Select : myExcel.Selection.ColumnWidth = 30
  95. xlSheet.Range("D3").Select() : myExcel.ActiveWindow.FreezePanes = True
  96. xlSheet.Range("A1:C1").Select()
  97. With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 14 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  98. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
  99. xlSheet.Range("A1:C1").Select()
  100. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  101. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  102. 位置1 = "A2:V" & N2 : xlSheet.Range(位置1).Select()
  103. myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  104. With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  105. With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  106. With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  107. With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  108. With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  109. With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  110. xlSheet.Range("A2:V2").Select()
  111. With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent2 : .TintAndShade = 0.799981688894314
  112. .PatternTintAndShade = 0 : End With
  113. xlSheet.Range("P12").Select()
  114. myExcel.ActiveWindow.DisplayGridlines = False
  115. End Sub
  116. End Class