説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

導出成EXCEL.vb 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 導出成EXCEL
  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 : Dim ds2 As New DataSet : Dim ds3 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. Dim 位置1 As String : Dim KI As Integer = 0
  22. Private Sub Set_DGV1載入前設定()
  23. DataGridView1.DataSource = Nothing : ds.Clear()
  24. DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  25. DataGridView1.ColumnHeadersHeight = 25
  26. DataGridView1.AllowUserToAddRows = False
  27. If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
  28. End Sub
  29. Private Sub Set_DGV1載入後設定()
  30. cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
  31. End Sub
  32. Private Sub Set_清單1()
  33. Set_DGV1載入前設定() : SQL1 = 列印用SQL : Set_DGV1載入後設定()
  34. End Sub
  35. Private Sub Set_格式設定()
  36. DataGridView1.Visible = False
  37. End Sub
  38. Private Sub 導出成EXCEL_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  39. FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
  40. Set_清單1() : Set_格式設定()
  41. End Sub
  42. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  43. 列印用SQL = "" : Me.Close()
  44. End Sub
  45. Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
  46. Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
  47. End Function
  48. Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
  49. xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
  50. xlApp.Visible = True
  51. xlApp.DisplayAlerts = True
  52. xlApp.Application.WindowState = xlMaximized
  53. xlBook = xlApp.Workbooks.Add
  54. '----------------------------------------------------------------------------------------------------------------------
  55. xlSheet = NewMethod(xlBook)
  56. If xlApp.Sheets(1).Name = "工作表2" Then
  57. xlApp.Sheets("工作表2").Select : xlApp.Sheets("工作表2").Name = "考勤資料"
  58. Else
  59. xlApp.Sheets("sheet2").Select : xlApp.Sheets("sheet2").Name = "考勤資料"
  60. End If
  61. xlBook.Activate()
  62. xlSheet.Activate()
  63. xlSheet.Cells(1, 1) = "东莞楠晋鞋业有限公司考勤" : xlSheet.Cells(2, 1) = "员工资料" : xlSheet.Cells(2, 3) = "考勤资料" : xlSheet.Cells(2, 5) = "第(1)班"
  64. xlSheet.Cells(2, 8) = "第(2)班" : xlSheet.Cells(2, 11) = "第(3)班" : xlSheet.Cells(2, 14) = "上班合计" : xlSheet.Cells(3, 1) = "序号" : xlSheet.Cells(3, 2) = "员工姓名"
  65. xlSheet.Cells(3, 3) = "出勤日期" : xlSheet.Cells(3, 4) = "星期" : xlSheet.Cells(3, 5) = "上班" : xlSheet.Cells(3, 6) = "下班" : xlSheet.Cells(3, 7) = "合计"
  66. xlSheet.Cells(3, 8) = "上班" : xlSheet.Cells(3, 9) = "下班" : xlSheet.Cells(3, 10) = "合计" : xlSheet.Cells(3, 11) = "上班" : xlSheet.Cells(3, 12) = "下班"
  67. xlSheet.Cells(3, 13) = "合计" : xlSheet.Cells(3, 14) = "上班时长" : xlSheet.Cells(3, 15) = "应上班"
  68. For i As Integer = 0 To DataGridView1.Rows.Count - 1
  69. xlSheet.Cells(i + 4, 1) = i + 1
  70. xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("員工名稱").Value
  71. xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("出勤日期").Value
  72. xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("星期").Value
  73. xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("第1班上班").Value
  74. xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("第1班下班").Value
  75. xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("第1班合計").Value
  76. xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("第2班上班").Value
  77. xlSheet.Cells(i + 4, 9) = DataGridView1.Rows(i).Cells("第2班下班").Value
  78. xlSheet.Cells(i + 4, 10) = DataGridView1.Rows(i).Cells("第2班合計").Value
  79. xlSheet.Cells(i + 4, 11) = DataGridView1.Rows(i).Cells("第3班上班").Value
  80. xlSheet.Cells(i + 4, 12) = DataGridView1.Rows(i).Cells("第3班下班").Value
  81. xlSheet.Cells(i + 4, 13) = DataGridView1.Rows(i).Cells("第3班合計").Value
  82. xlSheet.Cells(i + 4, 14) = DataGridView1.Rows(i).Cells("上班時長").Value
  83. xlSheet.Cells(i + 4, 15) = DataGridView1.Rows(i).Cells("應上班").Value
  84. KI = i + 5
  85. Next i
  86. xlSheet.Cells(KI, 2) = KI - 4 & "条记录"
  87. xlSheet.Cells(KI, 7) = "=SUM(G4:G" & KI - 1 & ")"
  88. xlSheet.Cells(KI, 10) = "=SUM(J4:J" & KI - 1 & ")"
  89. xlSheet.Cells(KI, 13) = "=SUM(M4:M" & KI - 1 & ")"
  90. xlSheet.Cells(KI, 14) = "=SUM(N4:N" & KI - 1 & ")"
  91. xlSheet.Cells(KI, 15) = "=SUM(O4:O" & KI - 1 & ")"
  92. AA(xlApp, xlSheet)
  93. If xlApp.Sheets(2).Name = "工作表1" Then : xlApp.Sheets("工作表1").Delete : Else : xlApp.Sheets("sheet1").Delete : End If
  94. xlSheet.PageSetup.PrintArea = ""
  95. xlApp.Cells.Select()
  96. xlApp.Application.WindowState = xlMinimized
  97. MsgBox("列印完成")
  98. Me.Close()
  99. End Sub
  100. Private Sub AA(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
  101. xlSheet.Cells.Select()
  102. With myExcel.Selection.Font : .Name = "Tahoma" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  103. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
  104. xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 3
  105. xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 7
  106. xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 9
  107. xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 4
  108. xlSheet.Columns("E:O").Select : myExcel.Selection.ColumnWidth = 9
  109. Dim DDS As String
  110. DDS = KI & ":" & KI
  111. xlSheet.Rows(DDS).Select : myExcel.Selection.RowHeight = 31
  112. xlSheet.Columns("A:A").Select
  113. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  114. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  115. xlSheet.Columns("C:C").Select
  116. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  117. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  118. xlSheet.Columns("D:D").Select
  119. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  120. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  121. xlSheet.Columns("E:O").Select
  122. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  123. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  124. xlSheet.Columns("G:G").Select : myExcel.Selection.Style = "Comma"
  125. xlSheet.Columns("J:J").Select : myExcel.Selection.Style = "Comma"
  126. xlSheet.Columns("M:O").Select : myExcel.Selection.Style = "Comma"
  127. xlSheet.Range("C" & KI & ":O" & KI & "").Select() : myExcel.Selection.NumberFormatLocal = "#,##0.0"
  128. xlSheet.Range("C" & KI & ":O" & KI & "").Select()
  129. With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  130. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  131. xlSheet.Range("A2:B2").Select()
  132. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  133. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  134. xlSheet.Range("C2:D2").Select()
  135. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  136. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  137. xlSheet.Range("E2:G2").Select()
  138. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  139. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  140. xlSheet.Range("H2:J2").Select()
  141. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  142. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  143. xlSheet.Range("K2:M2").Select()
  144. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  145. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  146. xlSheet.Range("N2:O2").Select()
  147. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  148. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  149. xlSheet.Range("A1:O1").Select()
  150. With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  151. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
  152. With myExcel.Selection.Font : .Size = 20 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
  153. .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
  154. xlSheet.Range("A2:O3").Select()
  155. With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorDark1 : .TintAndShade = -0.149998474074526
  156. .PatternTintAndShade = 0 : End With
  157. xlSheet.Range("A" & KI & ":O" & KI & "").Select()
  158. With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorDark1 : .TintAndShade = -0.149998474074526
  159. .PatternTintAndShade = 0 : End With
  160. xlSheet.Range("A2:O" & KI & "").Select()
  161. myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  162. With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  163. With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  164. With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  165. With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  166. With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  167. With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
  168. xlSheet.Rows("4:4").Select : myExcel.ActiveWindow.FreezePanes = True
  169. xlSheet.Rows("3:3").Select : myExcel.Selection.AutoFilter
  170. myExcel.ActiveWindow.DisplayGridlines = False
  171. xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 31
  172. With myExcel.Selection : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
  173. .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
  174. xlSheet.Range("T9").Select()
  175. End Sub
  176. End Class