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 xlApp As Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Office.Interop.Excel.Workbook Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim 判斷月差結果 As Boolean Dim Index_Row_Date As Date : Dim str01 As String = "" ReadOnly ds1 As New DataSet Private Sub 匯出EXCEL_Load(sender As Object, e As EventArgs) Handles MyBase.Load 匯出EXCEL_bt.Enabled = True FormBorderStyle = FormBorderStyle.SizableToolWindow 月選_起始_dtp.Value = Today() : 月選_結束_dtp.Value = Today() 月選_起始月_cb.Text = DateAndTime.Month(Today) : 月選_結束月_cb.Text = DateAndTime.Month(Today) 月選_起始年_cb.Text = DateAndTime.Year(Today) : 月選_結束年_cb.Text = DateAndTime.Year(Today) End Sub Private Sub 匯出EXCEL_Close(sender As Object, e As EventArgs) Handles MyBase.Closed 薪資匯出_dgv.DataSource = Nothing : ds1.Clear() : 顯示選擇月份_lb.Items.Clear() End Sub Private Sub 匯出EXCEL_bt_Click(sender As Object, e As EventArgs) Handles 匯出EXCEL_bt.Click 判斷月差結果 = 判斷月差() If 判斷月差結果 = True Then 薪資匯出SQL_to_DGV() Dim DGV_Row_index1 As Integer = 0 Dim DGV_Row_index2 As Integer = 0 SaveFileDialog1.Filter = "Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls" SaveFileDialog1.FilterIndex = 1 SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop SaveFileDialog1.FileName = "薪轉資料" + 薪轉Ex存檔名_tb.Text If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application) xlApp.Visible = True xlApp.DisplayAlerts = True xlApp.Application.WindowState = xlMaximized xlBook = xlApp.Workbooks.Add(misValue) Dim twC = New System.Globalization.TaiwanCalendar() Dim DTP3 As Date = 月選_結束_dtp.Value Index_Row_Date = DTP3 民國年月_tb.Text = twC.GetYear(DTP3).ToString + DTP3.ToString("MM") 計算月數_tb.Text = DateDiff(DateInterval.Month, 月選_起始_dtp.Value, 月選_結束_dtp.Value) For index_Conut As Integer = 0 To 計算月數_tb.Text 顯示選擇月份_lb.Items.Add(民國年月_tb.Text) Dim Sheet_Row_index As Integer = 4 xlSheet = NewMethod(xlBook) xlApp.Sheets(1).Name = 民國年月_tb.Text xlApp.Sheets(1).Select xlApp.Cells.NumberFormat = "@" '----------遇到數字,強迫以文字來儲存 xlBook.Activate() : xlSheet.Activate() xlSheet.Range("A1").Select() xlSheet.Cells(1, 1) = "雅邸家居國際有限公司" : xlSheet.Cells(2, 1) = twC.GetYear(Index_Row_Date).ToString & " 年 " & Index_Row_Date.ToString("MM") & " 月薪資轉帳" 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.Range("D4:F1000").Select() : xlApp.Selection.NumberFormatLocal = "#,##0;[紅色]-#,##0" Dim DGV_Row_Date As String = 薪資匯出_dgv(9, DGV_Row_index1).Value.ToString() Dim YY As String = Index_Row_Date.ToString("yyyy") + Index_Row_Date.ToString("/MM") + "/25" Do While String.Compare(DGV_Row_Date, YY) = 0 If String.Compare(薪資匯出_dgv(10, DGV_Row_index1).Value.ToString, "帳號匯款") = 0 Then xlSheet.Cells(Sheet_Row_index, 1) = 薪資匯出_dgv(1, DGV_Row_index1).Value.ToString xlSheet.Cells(Sheet_Row_index, 2) = 薪資匯出_dgv(2, DGV_Row_index1).Value.ToString xlSheet.Cells(Sheet_Row_index, 3) = 薪資匯出_dgv(3, DGV_Row_index1).Value.ToString xlSheet.Cells(Sheet_Row_index, 4) = 薪資匯出_dgv(4, DGV_Row_index1).Value.ToString xlSheet.Cells(Sheet_Row_index, 5) = 薪資匯出_dgv(5, DGV_Row_index1).Value.ToString xlSheet.Cells(Sheet_Row_index, 6) = "=SUM(D" & Sheet_Row_index & ": E" & Sheet_Row_index & ")" xlSheet.Cells(Sheet_Row_index, 7) = 薪資匯出_dgv(8, DGV_Row_index1).Value.ToString If DGV_Row_index1 < 薪資匯出_dgv.Rows.Count - 1 Then DGV_Row_index1 = DGV_Row_index1 + 1 Sheet_Row_index = Sheet_Row_index + 1 ElseIf DGV_Row_index1 = 薪資匯出_dgv.Rows.Count - 1 Then Exit Do End If Else If DGV_Row_index1 < 薪資匯出_dgv.Rows.Count - 1 Then DGV_Row_index1 = DGV_Row_index1 + 1 ElseIf DGV_Row_index1 = 薪資匯出_dgv.Rows.Count - 1 Then Exit Do End If End If DGV_Row_Date = 薪資匯出_dgv(9, DGV_Row_index1).Value.ToString() Loop xlSheet.Cells(Sheet_Row_index, 3) = "合計:" : xlSheet.Cells(Sheet_Row_index + 1, 3) = "領現:" xlSheet.Cells(Sheet_Row_index, 4) = "=SUM(D4: D" & (Sheet_Row_index - 1) & ")" xlSheet.Cells(Sheet_Row_index, 5) = "=SUM(E4: E" & (Sheet_Row_index - 1) & ")" xlSheet.Cells(Sheet_Row_index, 6) = "=SUM(F4: F" & (Sheet_Row_index - 1) & ")" ' 儲存格 格線設定 xlSheet.Range("A1:G" & (Sheet_Row_index - 1).ToString).Select() xlApp.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : xlApp.Selection.Borders(xlDiagonalUp).LineStyle = xlNone With xlApp.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With xlApp.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With xlApp.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With xlApp.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With xlApp.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With xlApp.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With xlSheet.Range("C" & Sheet_Row_index & ":F" & Sheet_Row_index).Select() xlApp.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : xlApp.Selection.Borders(xlDiagonalUp).LineStyle = xlNone : xlApp.Selection.Borders(xlEdgeLeft).LineStyle = xlNone With xlApp.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With xlApp.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With xlApp.Selection.Borders(xlEdgeRight).LineStyle = xlNone : xlApp.Selection.Borders(xlInsideVertical).LineStyle = xlNone : xlApp.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone ' ------------------------------------------------------- Dim Sheet_Row_index_暫存 As Integer = Sheet_Row_index Sheet_Row_index = Sheet_Row_index + 1 Dim 判斷有無領現 As Boolean = False Dim ww As String = 薪資匯出_dgv(9, DGV_Row_index2).Value.ToString() Dim zz As String = Index_Row_Date.ToString("yyyy") + Index_Row_Date.ToString("/MM") + "/25" Do While String.Compare(ww, zz) = 0 If String.Compare(薪資匯出_dgv(10, DGV_Row_index2).Value.ToString, "領現") = 0 Then 判斷有無領現 = True xlSheet.Cells(Sheet_Row_index, 6) = 薪資匯出_dgv(6, DGV_Row_index2).Value.ToString xlSheet.Cells(Sheet_Row_index, 7) = 薪資匯出_dgv(8, DGV_Row_index2).Value.ToString If DGV_Row_index2 < 薪資匯出_dgv.Rows.Count - 1 Then DGV_Row_index2 = DGV_Row_index2 + 1 Sheet_Row_index = Sheet_Row_index + 1 ElseIf DGV_Row_index2 = 薪資匯出_dgv.Rows.Count - 1 Then Sheet_Row_index = Sheet_Row_index + 1 Exit Do End If Else If DGV_Row_index2 < 薪資匯出_dgv.Rows.Count - 1 Then DGV_Row_index2 = DGV_Row_index2 + 1 ElseIf DGV_Row_index2 = 薪資匯出_dgv.Rows.Count - 1 Then Exit Do End If End If ww = 薪資匯出_dgv(9, DGV_Row_index2).Value.ToString() Loop xlSheet.Cells(Sheet_Row_index, 6) = "=SUM(F" & Sheet_Row_index_暫存 & ": F" & Sheet_Row_index - 1 & ")" xlSheet.Cells(Sheet_Row_index, 3) = "共計:" '加底線 xlSheet.Range("F" & Sheet_Row_index - 1 & ":F" & Sheet_Row_index - 1).Select() xlApp.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : xlApp.Selection.Borders(xlDiagonalUp).LineStyle = xlNone : xlApp.Selection.Borders(xlEdgeLeft).LineStyle = xlNone With xlApp.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With xlApp.Selection.Borders(xlEdgeRight).LineStyle = xlNone : xlApp.Selection.Borders(xlInsideVertical).LineStyle = xlNone : xlApp.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Index_Row_Date = DateAdd(DateInterval.Month, (index_Conut + 1) * (-1), DTP3) 民國年月_tb.Text = twC.GetYear(Index_Row_Date).ToString + Index_Row_Date.ToString("MM") ' ---------------------------------- '全選改變字體 xlSheet.Cells.Select() With xlApp.Selection.Font : .Name = "微軟正黑體" : .Size = 14 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone End With ' 改變行寬 Columns("A") & Columns("H") xlSheet.Columns("A:A").Select : xlApp.Selection.ColumnWidth = 8.38 xlSheet.Columns("B:B").Select : xlApp.Selection.ColumnWidth = 18 xlSheet.Columns("C:C").Select : xlApp.Selection.ColumnWidth = 16 xlSheet.Columns("D:D").Select : xlApp.Selection.ColumnWidth = 13 xlSheet.Columns("E:E").Select : xlApp.Selection.ColumnWidth = 13 xlSheet.Columns("F:F").Select : xlApp.Selection.ColumnWidth = 13 xlSheet.Columns("G:G").Select : xlApp.Selection.ColumnWidth = 15 ' ("A1到G1") 改變粗體 xlSheet.Range("A1:G3").Select() xlApp.Selection.Font.Bold = True '全選改變列高 xlSheet.Cells.Select() '全選 xlApp.Selection.RowHeight = 25 '改變列高 xlSheet.Range("A1").Select() ' ("A1到G1")跨欄至中 xlSheet.Range("A1:G1").Select() With xlApp.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With : xlApp.Selection.Merge ' ("A2到G2")跨欄至中 xlSheet.Range("A2:G2").Select() With xlApp.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False End With : xlApp.Selection.Merge ' 儲存格顏色設定 xlSheet.Range("A1:G1").Select() With xlApp.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent6 : .TintAndShade = 0.599993896298105 : .PatternTintAndShade = 0 : End With ' 儲存格顏色設定 xlSheet.Range("A2:G2").Select() With xlApp.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .Color = 16247773 : .TintAndShade = 0 : .PatternTintAndShade = 0 : End With Next xlApp.Sheets(CInt(計算月數_tb.Text) + 2).Delete xlApp.Cells.Select() xlApp.Application.WindowState = xlMinimized xlSheet.SaveAs(SaveFileDialog1.FileName) xlBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlBook) releaseObject(xlSheet) MessageBox.Show("存檔成功 !!!" & vbCrLf & "檔案儲存於 : " & SaveFileDialog1.FileName, "存檔資訊", MessageBoxButtons.OK, MessageBoxIcon.Information) End If 'Me.Close() Else 'MessageBox.Show("年/月 範圍選擇錯誤,請從新選擇", "範圍選擇提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If 匯出EXCEL_bt.Enabled = False 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 releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub Private Sub 月選_起始年_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 月選_起始年_cb.SelectedIndexChanged DTP = (月選_起始年_cb.Text) & "/" & (月選_起始月_cb.Text) & "/01" 月選_起始_dtp.Value = DTP 月選_起始_tb.Text = Strings.Left(Strings.Format(月選_起始_dtp.Value, "yyyy/MM/dd"), 10) End Sub Private Sub 月選_起始月_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 月選_起始月_cb.SelectedIndexChanged If 月選_起始年_cb.Text = "" Then Exit Sub Else DTP = (月選_起始年_cb.Text) & "/" & (月選_起始月_cb.Text) & "/01" 月選_起始_dtp.Value = DTP 月選_起始_tb.Text = Strings.Left(Strings.Format(月選_起始_dtp.Value, "yyyy/MM/dd"), 10) End If End Sub Private Sub 月選_結束年_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 月選_結束年_cb.SelectedIndexChanged DTP = (月選_結束年_cb.Text) & "/" & (月選_結束月_cb.Text) & "/01" 月選_結束_dtp.Value = DTP 月選_結束_dtp.Value = 月選_結束_dtp.Value.AddMonths(1) 月選_結束_dtp.Value = 月選_結束_dtp.Value.AddDays(-1) 月選_結束_tb.Text = Strings.Left(Strings.Format(月選_結束_dtp.Value, "yyyy/MM/dd"), 10) End Sub Private Sub 月選_結束月_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 月選_結束月_cb.SelectedIndexChanged If 月選_起始年_cb.Text = "" Then Exit Sub Else DTP = (月選_結束年_cb.Text) & "/" & (月選_結束月_cb.Text) & "/01" 月選_結束_dtp.Value = DTP 月選_結束_dtp.Value = 月選_結束_dtp.Value.AddMonths(1) 月選_結束_dtp.Value = 月選_結束_dtp.Value.AddDays(-1) 月選_結束_tb.Text = Strings.Left(Strings.Format(月選_結束_dtp.Value, "yyyy/MM/dd"), 10) End If End Sub Private Sub 結束減一個月_bt_Click(sender As Object, e As EventArgs) Handles 結束減一個月_bt.Click If 月選_結束月_cb.Text <> "1" Then 月選_結束月_cb.Text = Val(月選_結束月_cb.Text) - 1 ElseIf 月選_結束月_cb.Text = "1" Then 月選_結束月_cb.Text = 12 月選_結束年_cb.Text = Val(月選_結束年_cb.Text) - 1 End If End Sub Private Sub 結束加一個月_bt_Click(sender As Object, e As EventArgs) Handles 結束加一個月_bt.Click If 月選_結束月_cb.Text <> "12" Then 月選_結束月_cb.Text = Val(月選_結束月_cb.Text) + 1 ElseIf 月選_結束月_cb.Text = "12" Then 月選_結束月_cb.Text = 1 月選_結束年_cb.Text = Val(月選_結束年_cb.Text) + 1 End If End Sub Private Sub 起始減一個月_bt_Click(sender As Object, e As EventArgs) Handles 起始減一個月_bt.Click If 月選_起始月_cb.Text <> "1" Then 月選_起始月_cb.Text = Val(月選_起始月_cb.Text) - 1 ElseIf 月選_起始月_cb.Text = "1" Then 月選_起始月_cb.Text = 12 月選_起始年_cb.Text = Val(月選_起始年_cb.Text) - 1 End If End Sub Private Sub 起始加一個月_bt_Click(sender As Object, e As EventArgs) Handles 起始加一個月_bt.Click If 月選_起始月_cb.Text <> "12" Then 月選_起始月_cb.Text = Val(月選_起始月_cb.Text) + 1 ElseIf 月選_起始月_cb.Text = "12" Then 月選_起始月_cb.Text = 1 月選_起始年_cb.Text = Val(月選_起始年_cb.Text) + 1 End If End Sub Private Function 判斷月差() As Boolean Dim 判斷結果 As Boolean If DateDiff(DateInterval.Month, 月選_起始_dtp.Value, 月選_結束_dtp.Value) > 119 Then 月選_起始月_cb.Text = DateAndTime.Month(Today) : 月選_結束月_cb.Text = DateAndTime.Month(Today) 月選_起始年_cb.Text = DateAndTime.Year(Today) : 月選_結束年_cb.Text = DateAndTime.Year(Today) 判斷結果 = False MsgBox("選擇範圍超過10年,請從新選擇", MsgBoxStyle.Information, "提示") ElseIf DateDiff(DateInterval.Month, 月選_起始_dtp.Value, 月選_結束_dtp.Value) < 0 Then 月選_起始月_cb.Text = DateAndTime.Month(Today) : 月選_結束月_cb.Text = DateAndTime.Month(Today) 月選_起始年_cb.Text = DateAndTime.Year(Today) : 月選_結束年_cb.Text = DateAndTime.Year(Today) 判斷結果 = False MsgBox("選擇範圍年分順序顛倒,請從新選擇", MsgBoxStyle.Information, "提示") Else 判斷結果 = True End If Return 判斷結果 End Function Private Sub 薪資匯出SQL_to_DGV() 薪資匯出_dgv.DataSource = Nothing : ds1.Clear() 薪資匯出_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing 薪資匯出_dgv.ColumnHeadersHeight = 40 : 薪資匯出_dgv.AllowUserToAddRows = False str01 = "" : Dim DTP3 As Date = 月選_結束_dtp.Value 計算月數_tb.Text = DateDiff(DateInterval.Month, 月選_起始_dtp.Value, 月選_結束_dtp.Value) str01 = Strings.Left(Strings.Format(月選_結束_dtp.Value, "yyyy/MM/dd"), 7) + "/25" 薪資轉帳表_匯出EXCEL_月份清單(str01) da.Fill(ds1) : 薪資匯出_dgv.DataSource = ds1.Tables(0) : conn.Close() 薪資匯出_dgv.Columns("獎金").DefaultCellStyle.Format = "#,##0.0" 薪資匯出_dgv.Columns("薪資").DefaultCellStyle.Format = "#,##0.0" 薪資匯出_dgv.Columns("薪資合計").DefaultCellStyle.Format = "#,##0.0" 薪資匯出_dgv.Columns("領現").DefaultCellStyle.Format = "#,##0.0" For index_Conut As Integer = 0 To 計算月數_tb.Text 'ListBox1.Items.Add(str01) Index_Row_Date = DateAdd(DateInterval.Month, (index_Conut + 1) * (-1), DTP3) str01 = Strings.Left(Strings.Format(Index_Row_Date, "yyyy/MM/dd"), 7) + "/25" 薪資轉帳表_匯出EXCEL_月份清單(str01) da.Fill(ds1) : 薪資匯出_dgv.DataSource = ds1.Tables(0) : conn.Close() Next 薪資匯出_dgv.Columns(0).Visible = False : 薪資匯出_dgv.Columns(10).Visible = True : 薪資匯出_dgv.Columns(11).Visible = False 薪資匯出_dgv.Columns(0).ReadOnly = True : 薪資匯出_dgv.Columns(1).ReadOnly = True : 薪資匯出_dgv.Columns(2).ReadOnly = True : 薪資匯出_dgv.Columns(3).ReadOnly = True 薪資匯出_dgv.Columns(4).ReadOnly = True : 薪資匯出_dgv.Columns(5).ReadOnly = True : 薪資匯出_dgv.Columns(6).ReadOnly = True : 薪資匯出_dgv.Columns(7).ReadOnly = True 薪資匯出_dgv.Columns(8).ReadOnly = True : 薪資匯出_dgv.Columns(9).ReadOnly = True End Sub End Class