Nessuna descrizione
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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. Option Strict Off
  2. Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
  3. Imports Microsoft.Office.Interop.Excel.Constants
  4. Imports Microsoft.Office.Interop.Excel.XlBordersIndex
  5. Imports Microsoft.Office.Interop.Excel.XlLineStyle
  6. Imports Microsoft.Office.Interop.Excel.XlBorderWeight
  7. Imports Microsoft.Office.Interop.Excel.XlThemeFont
  8. Imports Microsoft.Office.Interop.Excel.XlThemeColor
  9. Imports Microsoft.Office.Interop.Excel.XlWindowState
  10. Public Class 列印清單
  11. ReadOnly ds As New DataSet
  12. Private Sub Set_DGV1載入前設定()
  13. DataGridView1.DataSource = Nothing : ds.Clear()
  14. DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  15. DataGridView1.ColumnHeadersHeight = 25
  16. DataGridView1.AllowUserToAddRows = False
  17. ConnOpen()
  18. End Sub
  19. Private Sub Set_DGV1載入後設定()
  20. CmdSet_For_DGV() : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
  21. End Sub
  22. Private Sub Set_庫存清單()
  23. Set_DGV1載入前設定() : SQL1 = 列印用SQL1 : Set_DGV1載入後設定()
  24. End Sub
  25. Private Sub Set_格式設定()
  26. DataGridView1.Visible = False
  27. End Sub
  28. Private Sub 列印清單_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  29. FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
  30. Set_庫存清單() : Set_格式設定()
  31. End Sub
  32. Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
  33. timeNow = 0
  34. GCM_ERP_SYS.Timer1.Enabled = False
  35. GCM_ERP_SYS.Timer1.Enabled = True
  36. End Sub
  37. Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
  38. timeNow = 0
  39. GCM_ERP_SYS.Timer1.Enabled = False
  40. GCM_ERP_SYS.Timer1.Enabled = True
  41. End Sub
  42. Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
  43. Dim xlApp As Microsoft.Office.Interop.Excel.Application
  44. Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
  45. Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
  46. xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
  47. xlBook = xlApp.Workbooks.Add
  48. xlApp.DisplayAlerts = True
  49. xlBook.Activate()
  50. xlSheet = NewMethod(xlBook)
  51. xlSheet.Activate()
  52. xlApp.Visible = True
  53. xlApp.Application.WindowState = xlMaximized
  54. If 列印(0) = True Then
  55. xlSheet.Cells(3, 1) = "零件編號"
  56. xlSheet.Cells(3, 2) = "進貨日期"
  57. xlSheet.Cells(3, 3) = "品牌"
  58. xlSheet.Cells(3, 4) = "品名"
  59. xlSheet.Cells(3, 5) = "型號及備註"
  60. xlSheet.Cells(3, 6) = "進貨單價"
  61. xlSheet.Cells(3, 7) = "庫存數量"
  62. xlSheet.Cells(3, 8) = "安全庫存"
  63. xlSheet.Cells(3, 9) = "庫存成本"
  64. xlSheet.Cells(3, 10) = "台幣庫存成本"
  65. xlSheet.Cells(3, 11) = "匯率"
  66. xlSheet.Cells(3, 12) = "供應商"
  67. xlSheet.Cells(1, 1) = "工巧明設備庫存清單" : xlSheet.Cells(1, 7) = "庫存總數" : xlSheet.Cells(1, 9) = "庫存總金额" : xlSheet.Cells(1, 10) = "台幣庫存總成本"
  68. 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)"
  69. For i As Integer = 0 To DataGridView1.Rows.Count - 1
  70. xlSheet.Cells(i + 4, 1) = DataGridView1.Rows(i).Cells("零件編號").Value
  71. xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("進貨日期").Value
  72. xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("品牌").Value
  73. xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("品名").Value
  74. xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("型號及備註").Value
  75. xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("進貨單價").Value
  76. xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("庫存數量").Value
  77. xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("安全庫存").Value
  78. xlSheet.Cells(i + 4, 9) = DataGridView1.Rows(i).Cells("庫存成本").Value
  79. xlSheet.Cells(i + 4, 10) = DataGridView1.Rows(i).Cells("台幣庫存成本").Value
  80. xlSheet.Cells(i + 4, 11) = DataGridView1.Rows(i).Cells("匯率").Value
  81. xlSheet.Cells(i + 4, 12) = DataGridView1.Rows(i).Cells("供應商").Value
  82. With xlApp.Selection
  83. .Font.Bold = True
  84. End With
  85. Next i
  86. ElseIf 列印(1) = True Then
  87. xlSheet.Cells(3, 1) = "零件編號"
  88. xlSheet.Cells(3, 2) = "結單日期"
  89. xlSheet.Cells(3, 3) = "品名"
  90. xlSheet.Cells(3, 4) = "型號及備註"
  91. xlSheet.Cells(3, 5) = "出貨數量"
  92. xlSheet.Cells(3, 6) = "進貨成本"
  93. xlSheet.Cells(3, 7) = "出貨金額"
  94. xlSheet.Cells(3, 8) = "利潤"
  95. xlSheet.Cells(3, 9) = "台幣利潤"
  96. xlSheet.Cells(1, 1) = "工巧明設備出庫紀錄清單"
  97. xlSheet.Cells(1, 5) = "出貨總數量" : xlSheet.Cells(1, 6) = "總成本" : xlSheet.Cells(1, 7) = "總金額" : xlSheet.Cells(1, 8) = "總利潤" : xlSheet.Cells(1, 9) = "台幣總利潤"
  98. 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)"
  99. xlSheet.Cells(2, 8) = "=SUBTOTAL(9,H4:H$99999)" : xlSheet.Cells(2, 9) = "=SUBTOTAL(9,I4:I$99999)"
  100. For i As Integer = 0 To DataGridView1.Rows.Count - 1
  101. xlSheet.Cells(i + 4, 1) = DataGridView1.Rows(i).Cells("零件編號").Value
  102. xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("結單日期").Value
  103. xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("品名").Value
  104. xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("型號及備註").Value
  105. xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("出貨數量").Value
  106. xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("進貨成本").Value
  107. xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("出貨金額").Value
  108. xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("利潤").Value
  109. xlSheet.Cells(i + 4, 9) = DataGridView1.Rows(i).Cells("台幣利潤").Value
  110. With xlApp.Selection
  111. .Font.Bold = True
  112. End With
  113. Next i
  114. End If
  115. BB(xlApp, xlSheet)
  116. xlSheet.PageSetup.PrintArea = ""
  117. xlApp.Cells.Select()
  118. xlSheet.Range("B1").Select()
  119. xlApp.Application.WindowState = xlMinimized
  120. MsgBox("列印完成")
  121. 列印(0) = False : 列印(1) = False : 列印用SQL1 = ""
  122. 列印用SQL1 = "" : Me.Close()
  123. End Sub
  124. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  125. 列印(0) = False : 列印(1) = False : 列印用SQL1 = ""
  126. 列印用SQL1 = "" : Me.Close()
  127. End Sub
  128. Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
  129. Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
  130. End Function
  131. Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
  132. Dim 位置1 As String
  133. If 列印(0) = True Then
  134. xlSheet.Cells.Select()
  135. With myExcel.Selection.Font
  136. .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  137. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
  138. End With
  139. xlSheet.Columns("F:H").Select : myExcel.Selection.NumberFormatLocal = "#,##0_ ;[紅色]-#,##0 "
  140. xlSheet.Columns("I:J").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00_ ;[紅色]-#,##0.00 "
  141. xlSheet.Columns("K:K").Select : myExcel.Selection.NumberFormatLocal = "0.0000_ ;[紅色]-0.0000 "
  142. xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 9
  143. xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 9.5
  144. xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 9
  145. xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 30
  146. xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 40
  147. xlSheet.Columns("F:L").Select : xlSheet.Columns("F:L").EntireColumn.AutoFit
  148. xlSheet.Rows("1:2").Select : myExcel.Selection.RowHeight = 15
  149. xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 30
  150. With myExcel.Selection
  151. .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  152. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  153. End With
  154. xlSheet.Range("A3:L3").Select() : myExcel.Selection.AutoFilter
  155. xlSheet.Rows("4:4").Select : myExcel.ActiveWindow.FreezePanes = True
  156. xlSheet.Range("A1:F2,H1:H2,K1:L2").Select()
  157. xlSheet.Range("K1").Activate()
  158. With myExcel.Selection
  159. .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  160. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  161. End With
  162. myExcel.Selection.Merge
  163. xlSheet.Range("A1:F2").Select()
  164. With myExcel.Selection.Font
  165. .Name = "微軟正黑體" : .Size = 14 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  166. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
  167. End With
  168. Dim M As Integer = 4
  169. For M = 4 To DataGridView1.Rows.Count + 2
  170. M += 1
  171. Next M
  172. 位置1 = "A1:L" & M
  173. xlSheet.Range(位置1).Select()
  174. myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  175. With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  176. With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  177. With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  178. With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  179. With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  180. With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  181. xlSheet.Range("A1:F2").Select() : myExcel.ActiveWindow.DisplayGridlines = False
  182. xlSheet.Columns("A:B").Select : xlSheet.Range("A3").Activate()
  183. With myExcel.Selection
  184. .HorizontalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext
  185. End With
  186. xlSheet.Range("A3:L3").Select()
  187. With myExcel.Selection
  188. .HorizontalAlignment = xlCenter : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  189. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  190. End With
  191. xlSheet.Range("A3:L3").Select()
  192. With myExcel.Selection.Interior
  193. .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent4 : .TintAndShade = 0.599993896298105 : .PatternTintAndShade = 0
  194. End With
  195. 位置1 = "C4:E" & M
  196. xlSheet.Range(位置1).Select()
  197. With myExcel.Selection
  198. .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  199. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  200. End With
  201. 位置1 = "F4:F" & M
  202. xlSheet.Range(位置1).Select()
  203. With myExcel.Selection
  204. .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  205. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  206. End With
  207. ElseIf 列印(1) = True Then
  208. xlSheet.Cells.Select()
  209. With myExcel.Selection.Font
  210. .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  211. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
  212. End With
  213. xlSheet.Columns("A:I").Select : xlSheet.Columns("A:I").EntireColumn.AutoFit
  214. xlSheet.Rows("1:2").Select : myExcel.Selection.RowHeight = 15
  215. xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 30
  216. xlSheet.Range("A3:I3").Select()
  217. With myExcel.Selection
  218. .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  219. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  220. End With
  221. myExcel.Selection.AutoFilter
  222. xlSheet.Columns("A:B").Select
  223. xlSheet.Range("B1").Activate() : myExcel.Selection.ColumnWidth = 10
  224. Dim M As Integer = 4
  225. For M = 4 To DataGridView1.Rows.Count + 2
  226. M += 1
  227. Next M
  228. 位置1 = "A1:I" & M
  229. xlSheet.Range(位置1).Select()
  230. myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  231. With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  232. With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  233. With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  234. With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  235. With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  236. With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  237. xlSheet.Rows("4:4").Select
  238. myExcel.ActiveWindow.FreezePanes = True : myExcel.ActiveWindow.DisplayGridlines = False
  239. xlSheet.Columns("C:C").Select : myExcel.Selection.NumberFormatLocal = "#,##0_ ;[紅色]-#,##0 "
  240. xlSheet.Columns("F:I").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00_ ;[紅色]-#,##0.00 "
  241. xlSheet.Columns("F:I").Select : myExcel.Selection.ColumnWidth = 12
  242. xlSheet.Range("A3:I3").Select()
  243. With myExcel.Selection.Interior
  244. .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent1 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0
  245. End With
  246. xlSheet.Range("A1:D2").Select()
  247. With myExcel.Selection
  248. .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  249. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  250. End With
  251. myExcel.Selection.Merge
  252. With myExcel.Selection.Font
  253. .Name = "微軟正黑體" : .Size = 14 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  254. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
  255. End With
  256. xlSheet.Range("A3:I3").Select()
  257. With myExcel.Selection
  258. .HorizontalAlignment = xlCenter : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  259. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  260. End With
  261. xlSheet.Range("E1:I1").Select()
  262. With myExcel.Selection
  263. .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  264. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
  265. End With
  266. End If
  267. End Sub
  268. End Class