暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

列印_零用金支付明細.vb 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 : Dim ds1 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載入前設定()
  33. If CheckBox1.Checked = True Then
  34. SQL1 = 列印用SQL
  35. Else
  36. SQL1 = "SELECT 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10) AS 申請日期, 零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10) AS 審核日期, " &
  37. "零用金支付表.核准人, LEFT(零用金支付表.核准日期, 10) AS 核准日期, 零用金支付明細.項次, 零用金支付明細.用途, 零用金支付明細.金額, 零用金支付明細.會計 AS 會計作業, " &
  38. "零用金支付明細.出納 AS 出納作業, 零用金支付明細.備註, 零用金支付表.會計, 零用金支付表.作帳日期, 零用金支付表.出納, 零用金支付表.出納日期, 零用金支付明細.現金, 零用金支付明細.備用金, " &
  39. "零用金支付明細.公帳 FROM 零用金支付表 RIGHT OUTER JOIN 零用金支付明細 " &
  40. "ON 零用金支付表.流水號 = 零用金支付明細.流水號 WHERE (零用金支付表.審核人 NOT LIKE N'作廢%') AND (零用金支付表.核准人 NOT LIKE N'') AND (零用金支付表.核准人 NOT LIKE N'作廢%') " &
  41. "AND (零用金支付表.核准日期 LIKE N'" & ComboBox1.Text & "%') " &
  42. " ORDER BY 零用金支付表.流水號 DESC, 零用金支付明細.項次"
  43. End If
  44. Set_DGV1載入後設定()
  45. End Sub
  46. Private Sub Set_格式設定()
  47. DataGridView1.Visible = False
  48. End Sub
  49. Private Sub ComboBox1下拉表單資料載入()
  50. conn.Close()
  51. If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
  52. SQL1 = "SELECT 年份 FROM 年份清單 ORDER BY 年份 DESC"
  53. cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
  54. ComboBox1.Items.Clear()
  55. While (dr.Read()) : ComboBox1.Items.Add(dr("年份")) : End While
  56. conn.Close()
  57. End Sub
  58. Private Sub 列印_零用金支付明細_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  59. FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
  60. ComboBox1下拉表單資料載入()
  61. CheckBox1.Checked = True : CheckBox2.Checked = False
  62. Set_清單() : Set_格式設定()
  63. End Sub
  64. Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
  65. xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
  66. xlBook = xlApp.Workbooks.Add
  67. xlApp.DisplayAlerts = True
  68. xlBook.Activate()
  69. xlSheet = NewMethod(xlBook)
  70. xlSheet.Activate()
  71. xlApp.Visible = True
  72. xlApp.Application.WindowState = xlMaximized
  73. xlSheet.Cells(1, 1) = "巨益貿易-零用金支付明細表" : xlSheet.Cells(2, 1) = "單號" : xlSheet.Cells(2, 2) = "部門" : xlSheet.Cells(2, 3) = "申請人" : xlSheet.Cells(2, 4) = "申請日期"
  74. xlSheet.Cells(2, 5) = "審核人" : xlSheet.Cells(2, 6) = "審核日期" : xlSheet.Cells(2, 7) = "核准人" : xlSheet.Cells(2, 8) = "核准日期" : xlSheet.Cells(2, 9) = "項次"
  75. xlSheet.Cells(2, 10) = "用途" : xlSheet.Cells(2, 11) = "金額" : xlSheet.Cells(2, 12) = "備註" : xlSheet.Cells(2, 13) = "會計" : xlSheet.Cells(2, 14) = "作帳日期" : xlSheet.Cells(2, 15) = "出納"
  76. xlSheet.Cells(2, 16) = "出納日期" : xlSheet.Cells(2, 17) = "現金" : xlSheet.Cells(2, 18) = "備用金" : xlSheet.Cells(2, 19) = "公帳"
  77. AA(xlApp, xlSheet)
  78. For i As Integer = 0 To DataGridView1.Rows.Count - 1
  79. xlSheet.Cells(i + 3, 1) = DataGridView1.Rows(i).Cells("流水號").Value : xlSheet.Cells(i + 3, 2) = DataGridView1.Rows(i).Cells("部門").Value
  80. xlSheet.Cells(i + 3, 3) = DataGridView1.Rows(i).Cells("申請人").Value : xlSheet.Cells(i + 3, 4) = DataGridView1.Rows(i).Cells("申請日期").Value
  81. xlSheet.Cells(i + 3, 5) = DataGridView1.Rows(i).Cells("審核人").Value : xlSheet.Cells(i + 3, 6) = DataGridView1.Rows(i).Cells("審核日期").Value
  82. xlSheet.Cells(i + 3, 7) = DataGridView1.Rows(i).Cells("核准人").Value : xlSheet.Cells(i + 3, 8) = DataGridView1.Rows(i).Cells("核准日期").Value
  83. xlSheet.Cells(i + 3, 9) = DataGridView1.Rows(i).Cells("項次").Value : xlSheet.Cells(i + 3, 10) = DataGridView1.Rows(i).Cells("用途").Value
  84. xlSheet.Cells(i + 3, 11) = DataGridView1.Rows(i).Cells("金額").Value : xlSheet.Cells(i + 3, 12) = DataGridView1.Rows(i).Cells("備註").Value
  85. xlSheet.Cells(i + 3, 13) = DataGridView1.Rows(i).Cells("會計").Value : xlSheet.Cells(i + 3, 14) = DataGridView1.Rows(i).Cells("作帳日期").Value
  86. xlSheet.Cells(i + 3, 15) = DataGridView1.Rows(i).Cells("出納").Value : xlSheet.Cells(i + 3, 16) = DataGridView1.Rows(i).Cells("出納日期").Value
  87. xlSheet.Cells(i + 3, 17) = DataGridView1.Rows(i).Cells("現金").Value : xlSheet.Cells(i + 3, 18) = DataGridView1.Rows(i).Cells("備用金").Value
  88. xlSheet.Cells(i + 3, 19) = DataGridView1.Rows(i).Cells("公帳").Value
  89. With xlApp.Selection
  90. .Font.Bold = True
  91. End With
  92. N2 = i + 3 : N1 = i
  93. Next i
  94. BB(xlApp, xlSheet)
  95. xlSheet.PageSetup.PrintArea = ""
  96. xlApp.Cells.Select()
  97. xlSheet.Range("B1").Select()
  98. xlApp.Application.WindowState = xlMinimized
  99. MsgBox("列印完成")
  100. Me.Close()
  101. End Sub
  102. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  103. 列印用SQL = "" : Me.Close()
  104. End Sub
  105. Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
  106. Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
  107. End Function
  108. Private Sub AA(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
  109. xlSheet.Cells.Select()
  110. With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  111. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
  112. xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 30
  113. xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 25
  114. With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
  115. .ReadingOrder = xlContext : .MergeCells = False : End With
  116. myExcel.Selection.AutoFilter
  117. xlSheet.Range("A1").Select()
  118. With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 20 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  119. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
  120. xlSheet.Rows("3:3").Select
  121. myExcel.ActiveWindow.FreezePanes = True
  122. xlSheet.Range("A2:S2").Select()
  123. myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  124. With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  125. With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  126. With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  127. With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  128. With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  129. With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  130. myExcel.ActiveWindow.DisplayGridlines = False
  131. xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 15.5
  132. xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 11.5
  133. xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 6
  134. xlSheet.Columns("D:D").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10
  135. xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 6
  136. xlSheet.Columns("F:F").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10
  137. xlSheet.Columns("G:G").Select : myExcel.Selection.ColumnWidth = 6
  138. xlSheet.Columns("H:H").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10
  139. xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 4 : myExcel.Selection.NumberFormatLocal = "@"
  140. xlSheet.Columns("J:J").Select : myExcel.Selection.ColumnWidth = 52
  141. xlSheet.Columns("K:K").Select : myExcel.Selection.ColumnWidth = 12 : myExcel.Selection.Style = "Comma"
  142. xlSheet.Columns("L:L").Select : myExcel.Selection.ColumnWidth = 45
  143. xlSheet.Columns("M:M").Select : myExcel.Selection.ColumnWidth = 6
  144. xlSheet.Columns("N:N").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10
  145. xlSheet.Columns("O:O").Select : myExcel.Selection.ColumnWidth = 6
  146. xlSheet.Columns("P:P").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10
  147. xlSheet.Columns("Q:S").Select : myExcel.Selection.ColumnWidth = 8
  148. xlSheet.Cells.Select() : myExcel.ActiveWindow.Zoom = 90
  149. End Sub
  150. Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
  151. Dim 位置1 As String
  152. 位置1 = "A3:S" & N1 + 3 : xlSheet.Range(位置1).Select()
  153. myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  154. With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  155. With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  156. With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  157. With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  158. With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  159. With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  160. End Sub
  161. Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
  162. CheckBox1.Checked = True : CheckBox2.Checked = False
  163. Set_清單()
  164. End Sub
  165. Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.Click
  166. CheckBox1.Checked = False : CheckBox2.Checked = True
  167. Set_清單()
  168. End Sub
  169. Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
  170. Set_清單()
  171. End Sub
  172. End Class