123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- Option Strict Off
- Imports System.Data.SqlClient
- Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
- Imports Microsoft.Office.Interop.Excel.Constants
- Imports Microsoft.Office.Interop.Excel.XlBordersIndex
- Imports Microsoft.Office.Interop.Excel.XlLineStyle
- Imports Microsoft.Office.Interop.Excel.XlBorderWeight
- Imports Microsoft.Office.Interop.Excel.XlThemeFont
- Imports Microsoft.Office.Interop.Excel.XlThemeColor
- Imports Microsoft.Office.Interop.Excel.XlWindowState
- Public Class 導出成EXCEL
- Dim conn As New SqlConnection
- Dim da As New SqlDataAdapter
- Dim cmd As New SqlCommand
- Dim ds As New DataSet : Dim ds1 As New DataSet : Dim ds2 As New DataSet : Dim ds3 As New DataSet
- Dim dr As SqlDataReader
- Dim N1 As Integer : Dim N2 As Integer
- Dim xlApp As Microsoft.Office.Interop.Excel.Application
- Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
- Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
- Dim 位置1 As String : Dim KI As Integer = 0
- Private Sub Set_DGV1載入前設定()
- DataGridView1.DataSource = Nothing : ds.Clear()
- DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
- DataGridView1.ColumnHeadersHeight = 25
- DataGridView1.AllowUserToAddRows = False
- If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
- End Sub
- Private Sub Set_DGV1載入後設定()
- cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
- End Sub
- Private Sub Set_清單1()
- Set_DGV1載入前設定() : SQL1 = 列印用SQL : Set_DGV1載入後設定()
- End Sub
- Private Sub Set_格式設定()
- DataGridView1.Visible = False
- End Sub
- Private Sub 導出成EXCEL_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
- Set_清單1() : Set_格式設定()
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- 列印用SQL = "" : Me.Close()
- End Sub
- Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
- Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
- End Function
- Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
- xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
- xlApp.Visible = True
- xlApp.DisplayAlerts = True
- xlApp.Application.WindowState = xlMaximized
- xlBook = xlApp.Workbooks.Add
- '----------------------------------------------------------------------------------------------------------------------
- xlSheet = NewMethod(xlBook)
- If xlApp.Sheets(1).Name = "工作表2" Then
- xlApp.Sheets("工作表2").Select : xlApp.Sheets("工作表2").Name = "考勤資料"
- Else
- xlApp.Sheets("sheet2").Select : xlApp.Sheets("sheet2").Name = "考勤資料"
- End If
- xlBook.Activate()
- xlSheet.Activate()
-
- xlSheet.Cells(1, 1) = "东莞楠晋鞋业有限公司考勤" : xlSheet.Cells(2, 1) = "员工资料" : xlSheet.Cells(2, 3) = "考勤资料" : xlSheet.Cells(2, 5) = "第(1)班"
- xlSheet.Cells(2, 8) = "第(2)班" : xlSheet.Cells(2, 11) = "第(3)班" : xlSheet.Cells(2, 14) = "上班合计" : xlSheet.Cells(3, 1) = "序号" : xlSheet.Cells(3, 2) = "员工姓名"
- xlSheet.Cells(3, 3) = "出勤日期" : xlSheet.Cells(3, 4) = "星期" : xlSheet.Cells(3, 5) = "上班" : xlSheet.Cells(3, 6) = "下班" : xlSheet.Cells(3, 7) = "合计"
- xlSheet.Cells(3, 8) = "上班" : xlSheet.Cells(3, 9) = "下班" : xlSheet.Cells(3, 10) = "合计" : xlSheet.Cells(3, 11) = "上班" : xlSheet.Cells(3, 12) = "下班"
- xlSheet.Cells(3, 13) = "合计" : xlSheet.Cells(3, 14) = "上班时长" : xlSheet.Cells(3, 15) = "应上班"
-
- For i As Integer = 0 To DataGridView1.Rows.Count - 1
- xlSheet.Cells(i + 4, 1) = i + 1
- xlSheet.Cells(i + 4, 2) = DataGridView1.Rows(i).Cells("員工名稱").Value
- xlSheet.Cells(i + 4, 3) = DataGridView1.Rows(i).Cells("出勤日期").Value
- xlSheet.Cells(i + 4, 4) = DataGridView1.Rows(i).Cells("星期").Value
- xlSheet.Cells(i + 4, 5) = DataGridView1.Rows(i).Cells("第1班上班").Value
- xlSheet.Cells(i + 4, 6) = DataGridView1.Rows(i).Cells("第1班下班").Value
- xlSheet.Cells(i + 4, 7) = DataGridView1.Rows(i).Cells("第1班合計").Value
- xlSheet.Cells(i + 4, 8) = DataGridView1.Rows(i).Cells("第2班上班").Value
- xlSheet.Cells(i + 4, 9) = DataGridView1.Rows(i).Cells("第2班下班").Value
- xlSheet.Cells(i + 4, 10) = DataGridView1.Rows(i).Cells("第2班合計").Value
- xlSheet.Cells(i + 4, 11) = DataGridView1.Rows(i).Cells("第3班上班").Value
- xlSheet.Cells(i + 4, 12) = DataGridView1.Rows(i).Cells("第3班下班").Value
- xlSheet.Cells(i + 4, 13) = DataGridView1.Rows(i).Cells("第3班合計").Value
- xlSheet.Cells(i + 4, 14) = DataGridView1.Rows(i).Cells("上班時長").Value
- xlSheet.Cells(i + 4, 15) = DataGridView1.Rows(i).Cells("應上班").Value
- KI = i + 5
- Next i
-
- xlSheet.Cells(KI, 2) = KI - 4 & "条记录"
- xlSheet.Cells(KI, 7) = "=SUM(G4:G" & KI - 1 & ")"
- xlSheet.Cells(KI, 10) = "=SUM(J4:J" & KI - 1 & ")"
- xlSheet.Cells(KI, 13) = "=SUM(M4:M" & KI - 1 & ")"
- xlSheet.Cells(KI, 14) = "=SUM(N4:N" & KI - 1 & ")"
- xlSheet.Cells(KI, 15) = "=SUM(O4:O" & KI - 1 & ")"
-
- AA(xlApp, xlSheet)
-
- If xlApp.Sheets(2).Name = "工作表1" Then : xlApp.Sheets("工作表1").Delete : Else : xlApp.Sheets("sheet1").Delete : End If
- xlSheet.PageSetup.PrintArea = ""
- xlApp.Cells.Select()
- xlApp.Application.WindowState = xlMinimized
- MsgBox("列印完成")
- Me.Close()
- End Sub
-
- Private Sub AA(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
- xlSheet.Cells.Select()
- With myExcel.Selection.Font : .Name = "Tahoma" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
- .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
-
- xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 3
- xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 7
- xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 9
- xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 4
- xlSheet.Columns("E:O").Select : myExcel.Selection.ColumnWidth = 9
- Dim DDS As String
- DDS = KI & ":" & KI
- xlSheet.Rows(DDS).Select : myExcel.Selection.RowHeight = 31
-
- xlSheet.Columns("A:A").Select
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
-
- xlSheet.Columns("C:C").Select
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
-
- xlSheet.Columns("D:D").Select
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
-
- xlSheet.Columns("E:O").Select
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
-
- xlSheet.Columns("G:G").Select : myExcel.Selection.Style = "Comma"
- xlSheet.Columns("J:J").Select : myExcel.Selection.Style = "Comma"
- xlSheet.Columns("M:O").Select : myExcel.Selection.Style = "Comma"
-
- xlSheet.Range("C" & KI & ":O" & KI & "").Select() : myExcel.Selection.NumberFormatLocal = "#,##0.0"
- xlSheet.Range("C" & KI & ":O" & KI & "").Select()
- With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
-
- xlSheet.Range("A2:B2").Select()
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
- xlSheet.Range("C2:D2").Select()
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
- xlSheet.Range("E2:G2").Select()
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
- xlSheet.Range("H2:J2").Select()
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
- xlSheet.Range("K2:M2").Select()
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
- xlSheet.Range("N2:O2").Select()
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
- xlSheet.Range("A1:O1").Select()
- With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
- With myExcel.Selection.Font : .Size = 20 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
- .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
-
- xlSheet.Range("A2:O3").Select()
- With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorDark1 : .TintAndShade = -0.149998474074526
- .PatternTintAndShade = 0 : End With
-
- xlSheet.Range("A" & KI & ":O" & KI & "").Select()
- With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorDark1 : .TintAndShade = -0.149998474074526
- .PatternTintAndShade = 0 : End With
-
- xlSheet.Range("A2:O" & KI & "").Select()
- myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
- With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
- With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
- With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
- With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
- With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
- With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
-
- xlSheet.Rows("4:4").Select : myExcel.ActiveWindow.FreezePanes = True
- xlSheet.Rows("3:3").Select : myExcel.Selection.AutoFilter
- myExcel.ActiveWindow.DisplayGridlines = False
- xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 31
- With myExcel.Selection : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
- .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
- xlSheet.Range("T9").Select()
- End Sub
- End Class
|