暫無描述
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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. Option Strict Off
  2. Imports System.Data.SqlClient
  3. Imports System.IO
  4. Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
  5. Imports Microsoft.Office.Interop.Excel.Constants
  6. Imports Microsoft.Office.Interop.Excel.XlBordersIndex
  7. Imports Microsoft.Office.Interop.Excel.XlLineStyle
  8. Imports Microsoft.Office.Interop.Excel.XlBorderWeight
  9. Imports Microsoft.Office.Interop.Excel.XlThemeFont
  10. Imports Microsoft.Office.Interop.Excel.XlThemeColor
  11. Imports Microsoft.Office.Interop.Excel.XlWindowState
  12. Imports Microsoft.Office.Interop.Excel
  13. Public Class 列印_零用金支付單
  14. Dim conn As New SqlConnection
  15. Dim da As New SqlDataAdapter
  16. Dim cmd As New SqlCommand
  17. Dim ds1 As New DataSet : Dim ds2 As New DataSet : Dim ds3 As New DataSet : Dim ds4 As New DataSet
  18. Dim dr As SqlDataReader
  19. Dim N1 As Integer : Dim N2 As Integer
  20. Dim xlApp As Application
  21. Dim xlBook As Workbook
  22. Dim xlSheet As Worksheet
  23. Dim 位置1 As String
  24. Private Sub Set_DGV1載入前設定()
  25. DataGridView1.DataSource = Nothing : ds1.Clear()
  26. DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  27. DataGridView1.ColumnHeadersHeight = 25
  28. DataGridView1.AllowUserToAddRows = False
  29. If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
  30. End Sub
  31. Private Sub Set_DGV1載入後設定()
  32. cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView1.DataSource = ds1.Tables(0) : conn.Close()
  33. End Sub
  34. Private Sub Set_訂單清單1()
  35. Set_DGV1載入前設定()
  36. SQL1 = "SELECT 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10) AS 申請日期,
  37. 零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10) AS 審核日期, 零用金支付表.核准人,
  38. LEFT(零用金支付表.核准日期, 10) AS 核准日期, 零用金支付表.核准圖檔, 零用金支付表.簽名編碼,
  39. 零用金支付明細.出納 AS 已出納
  40. FROM 零用金支付表 INNER JOIN
  41. 零用金支付明細 ON 零用金支付表.流水號 = 零用金支付明細.流水號
  42. WHERE (零用金支付表.流水號 LIKE N'" & 列印用SQL & "') AND (零用金支付表.審核人 NOT LIKE N'作廢%') AND (零用金支付表.審核人 NOT LIKE N'') AND
  43. (零用金支付表.核准人 NOT LIKE N'作廢%') AND (零用金支付表.核准人 NOT LIKE N'')
  44. GROUP BY 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10),
  45. 零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10), 零用金支付表.核准人, LEFT(零用金支付表.核准日期, 10),
  46. 零用金支付表.核准圖檔, 零用金支付表.簽名編碼, 零用金支付明細.出納
  47. ORDER BY 零用金支付表.流水號 DESC"
  48. Set_DGV1載入後設定()
  49. End Sub
  50. Private Sub Set_DGV2載入前設定()
  51. DataGridView2.DataSource = Nothing : ds2.Clear()
  52. DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  53. DataGridView2.ColumnHeadersHeight = 25
  54. DataGridView2.AllowUserToAddRows = False
  55. If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
  56. End Sub
  57. Private Sub Set_訂單清單2()
  58. Set_DGV2載入前設定()
  59. SQL1 = 列印用SQL2
  60. Set_DGV2載入後設定()
  61. End Sub
  62. Private Sub Set_DGV2載入後設定()
  63. cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView2.DataSource = ds2.Tables(0) : conn.Close()
  64. End Sub
  65. Private Sub Set_格式設定()
  66. DataGridView1.Visible = False : DataGridView2.Visible = False
  67. PictureBox1.Visible = False
  68. End Sub
  69. Private Sub 列印_零用金支付單_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  70. Set_格式設定()
  71. FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
  72. If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
  73. SQL1 = "SELECT 簽名檔圖片 FROM 簽名檔管理 WHERE 圖片流水號 LIKE N'" & 簽核圖片編號 & "'"
  74. cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
  75. While dr.Read() = True
  76. Dim bytes As Byte() = New Byte(-1) {}
  77. bytes = DirectCast(dr.Item("簽名檔圖片"), Byte())
  78. Dim oStream As New MemoryStream(bytes)
  79. PictureBox1.Image = Bitmap.FromStream(oStream)
  80. End While
  81. conn.Close()
  82. Set_訂單清單1() : Set_訂單清單2()
  83. End Sub
  84. Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
  85. xlApp = CType(CreateObject("Excel.Application"), Application)
  86. xlBook = xlApp.Workbooks.Add
  87. xlApp.DisplayAlerts = True
  88. xlBook.Activate()
  89. xlSheet = NewMethod(xlBook)
  90. xlSheet.Activate()
  91. xlApp.Visible = True
  92. xlApp.Application.WindowState = xlMaximized
  93. xlSheet.Cells(1, 1) = "費用報銷單" : xlSheet.Cells(2, 1) = "報銷部門 : " : xlSheet.Cells(2, 4) = "申請人 : " : xlSheet.Cells(2, 6) = "申請日期 : " : xlSheet.Cells(3, 1) = "項次"
  94. xlSheet.Cells(3, 2) = "用途" : xlSheet.Cells(3, 5) = "金額" : xlSheet.Cells(3, 6) = "備註"
  95. xlSheet.Cells(2, 3) = DataGridView1.Rows(0).Cells("部門").Value : xlSheet.Cells(2, 5) = DataGridView1.Rows(0).Cells("申請人").Value
  96. xlSheet.Cells(2, 7) = DataGridView1.Rows(0).Cells("申請日期").Value
  97. For i As Integer = 0 To DataGridView2.Rows.Count - 1
  98. xlSheet.Cells(i + 4, 1) = i + 1
  99. xlSheet.Cells(i + 4, 2) = DataGridView2.Rows(i).Cells("用途").Value
  100. xlSheet.Cells(i + 4, 5) = DataGridView2.Rows(i).Cells("金額").Value
  101. xlSheet.Cells(i + 4, 6) = DataGridView2.Rows(i).Cells("備註").Value
  102. With xlApp.Selection
  103. .Font.Bold = True
  104. End With
  105. N1 = i + 4 : N2 = i + 4
  106. Next i
  107. xlSheet.Cells(N1 + 1, 2) = "以下空白" : xlSheet.Cells(N1 + 3, 4) = "合计" : xlSheet.Cells(N1 + 3, 5) = "=SUM(E4:E" & N1 + 1 & ")"
  108. xlSheet.Cells(N1 + 3, 1) = "單據驗證碼 : " : xlSheet.Cells(N1 + 4, 1) = "核准人 : " : xlSheet.Cells(N1 + 4, 4) = "審核人 : " : xlSheet.Cells(N1 + 4, 6) = "審核日期 : "
  109. xlSheet.Cells(N1 + 5, 4) = "會計 : " : xlSheet.Cells(N1 + 5, 6) = "對帳日期 : " : xlSheet.Cells(N1 + 6, 4) = "出納 : " : xlSheet.Cells(N1 + 6, 6) = "出納日期 : "
  110. xlSheet.Cells(N1 + 6, 8) = "領款人 : "
  111. xlSheet.Cells(N1 + 3, 3) = DataGridView1.Rows(0).Cells("簽名編碼").Value : xlSheet.Cells(N1 + 4, 5) = DataGridView1.Rows(0).Cells("審核人").Value
  112. xlSheet.Cells(N1 + 4, 7) = DataGridView1.Rows(0).Cells("審核日期").Value
  113. AA(xlApp, xlSheet) : BB(xlApp, xlSheet) : CC(xlApp, xlSheet)
  114. xlSheet.PageSetup.PrintArea = ""
  115. xlApp.Cells.Select()
  116. xlSheet.Range("B1").Select()
  117. xlApp.Application.WindowState = xlMinimized
  118. xlApp.ActiveSheet.Protect(DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="0911274990")
  119. xlApp.ActiveSheet.EnableSelection = 1
  120. MsgBox("列印完成")
  121. Me.Close()
  122. End Sub
  123. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  124. 列印用SQL = "" : Me.Close()
  125. End Sub
  126. Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
  127. Return CType(xlBook.Worksheets.Add, Worksheet)
  128. End Function
  129. Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
  130. xlSheet.Cells.Select()
  131. With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False
  132. .Superscript = False
  133. .Subscript = False
  134. .OutlineFont = False
  135. .Shadow = False
  136. .Underline = xlUnderlineStyleNone
  137. .ThemeColor = xlThemeColorLight1
  138. .TintAndShade = 0
  139. .ThemeFont = xlThemeFontNone
  140. End With
  141. xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 5
  142. xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 9
  143. xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 25
  144. xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 9
  145. xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 15
  146. xlSheet.Columns("F:F").Select : myExcel.Selection.ColumnWidth = 10
  147. xlSheet.Columns("G:G").Select : myExcel.Selection.ColumnWidth = 12
  148. xlSheet.Columns("H:H").Select : myExcel.Selection.ColumnWidth = 8
  149. xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 10
  150. xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 35
  151. xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 30
  152. xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 20
  153. xlSheet.Rows("4:" & N2 + 2 & "").Select : myExcel.Selection.RowHeight = 30
  154. xlSheet.Rows("" & N2 + 3 & ":" & N2 + 6 & "").Select : myExcel.Selection.RowHeight = 30
  155. 位置1 = "A3:I" & N2 + 2 : xlSheet.Range(位置1).Select()
  156. myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  157. With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
  158. With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
  159. With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
  160. With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
  161. myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone
  162. With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
  163. xlSheet.Range("A1:I1").Select() : myExcel.Selection.Merge
  164. With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 20 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  165. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
  166. xlSheet.Range("A2:I2").Select()
  167. With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  168. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  169. myExcel.ActiveWindow.DisplayGridlines = False
  170. 位置1 = "A2:A" & N2 + 4 : xlSheet.Range(位置1).Select()
  171. With myExcel.Selection : .HorizontalAlignment = xlLeft : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
  172. .ReadingOrder = xlContext : .MergeCells = False : End With
  173. 位置1 = "D2:D" & N2 + 4 : xlSheet.Range(位置1).Select()
  174. With myExcel.Selection : .HorizontalAlignment = xlLeft : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
  175. .ReadingOrder = xlContext : .MergeCells = False : End With
  176. 位置1 = "E2:E" & N2 + 4 : xlSheet.Range(位置1).Select()
  177. myExcel.Selection.Style = "Comma"
  178. With myExcel.Selection : .HorizontalAlignment = xlRight : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
  179. .ReadingOrder = xlContext : .MergeCells = False : End With
  180. xlSheet.Range("E2").Select()
  181. With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  182. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  183. 位置1 = "E" & N2 + 4 : xlSheet.Range(位置1).Select()
  184. With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  185. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  186. 位置1 = "G" & N2 + 4 : xlSheet.Range(位置1).Select()
  187. With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  188. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  189. 位置1 = "F" & N2 + 4 : xlSheet.Range(位置1).Select()
  190. With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  191. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  192. 位置1 = "F" & N2 + 5 : xlSheet.Range(位置1).Select()
  193. With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  194. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  195. 位置1 = "F" & N2 + 6 : xlSheet.Range(位置1).Select()
  196. With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  197. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  198. 位置1 = "H" & N2 + 4 : xlSheet.Range(位置1).Select()
  199. With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  200. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  201. xlSheet.Range("G2").Select()
  202. With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  203. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  204. xlSheet.Range("F2").Select()
  205. With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  206. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  207. xlSheet.Range("A1:I1").Select()
  208. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
  209. .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With
  210. xlSheet.Range("F2:G2").Select()
  211. With myExcel.Selection : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
  212. .ReadingOrder = xlContext : .MergeCells = False : End With
  213. For I As Integer = 4 To N2
  214. xlSheet.Range("B" & I & ":D" & I).Select()
  215. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  216. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  217. myExcel.Selection.Merge
  218. With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False
  219. .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With
  220. Next
  221. End Sub
  222. Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
  223. Clipboard.SetDataObject(PictureBox1.Image)
  224. 位置1 = "C" & N2 + 4 : xlSheet.Range(位置1).Select()
  225. myExcel.ActiveSheet.Pictures.Paste.Select
  226. myExcel.Selection.ShapeRange.Height = 43
  227. End Sub
  228. Private Sub CC(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
  229. myExcel.Application.PrintCommunication = False
  230. With myExcel.ActiveSheet.PageSetup
  231. .PrintTitleRows = ""
  232. .PrintTitleColumns = ""
  233. End With
  234. myExcel.Application.PrintCommunication = True
  235. myExcel.ActiveSheet.PageSetup.PrintArea = ""
  236. myExcel.Application.PrintCommunication = False
  237. With myExcel.ActiveSheet.PageSetup
  238. .LeftHeader = ""
  239. .CenterHeader = ""
  240. .RightHeader = ""
  241. .LeftFooter = ""
  242. .CenterFooter = ""
  243. .RightFooter = ""
  244. .LeftMargin = myExcel.Application.InchesToPoints(0.25)
  245. .RightMargin = myExcel.Application.InchesToPoints(0.25)
  246. .TopMargin = myExcel.Application.InchesToPoints(0.75)
  247. .BottomMargin = myExcel.Application.InchesToPoints(0.75)
  248. .HeaderMargin = myExcel.Application.InchesToPoints(0.3)
  249. .FooterMargin = myExcel.Application.InchesToPoints(0.3)
  250. .PrintHeadings = False
  251. .PrintGridlines = False
  252. .PrintQuality = 600
  253. .CenterHorizontally = False
  254. .CenterVertically = False
  255. .Draft = False
  256. .FirstPageNumber = xlAutomatic
  257. .BlackAndWhite = False
  258. .Zoom = False
  259. .FitToPagesWide = 1
  260. .FitToPagesTall = 1
  261. .OddAndEvenPagesHeaderFooter = False
  262. .DifferentFirstPageHeaderFooter = False
  263. .ScaleWithDocHeaderFooter = True
  264. .AlignMarginsHeaderFooter = True
  265. .EvenPage.LeftHeader.Text = ""
  266. .EvenPage.CenterHeader.Text = ""
  267. .EvenPage.RightHeader.Text = ""
  268. .EvenPage.LeftFooter.Text = ""
  269. .EvenPage.CenterFooter.Text = ""
  270. .EvenPage.RightFooter.Text = ""
  271. .FirstPage.LeftHeader.Text = ""
  272. .FirstPage.CenterHeader.Text = ""
  273. .FirstPage.RightHeader.Text = ""
  274. .FirstPage.LeftFooter.Text = ""
  275. .FirstPage.CenterFooter.Text = ""
  276. .FirstPage.RightFooter.Text = ""
  277. End With
  278. myExcel.Application.PrintCommunication = True
  279. End Sub
  280. End Class