Imports System.IO
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
Imports Microsoft.Office.Interop.Excel.XlPageOrientation
Imports Microsoft.Office.Interop.Excel
Public Class 加班申請資料
    Dim 剛開啟 As Boolean : ReadOnly ds1 As New DataSet : Dim 位置1 As Integer
    Dim xlApp As Application : Dim xlBook As Workbook : Dim xlSheet As Worksheet
    Private Sub Set_加班明細()
        月清單_DGV.DataSource = Nothing : ds1.Clear()
        月清單_DGV.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        月清單_DGV.ColumnHeadersHeight = 40
        月清單_DGV.AllowUserToAddRows = False
        PA = 年份_cb.Text & "/" & 月份_cb.Text : SQL_加班申請清單()
        da.Fill(ds1) : 月清單_DGV.DataSource = ds1.Tables(0) : conn.Close()

        月清單_DGV.Columns(1).Visible = False
        月清單_DGV.Columns(0).FillWeight = 60 : 月清單_DGV.Columns(2).FillWeight = 70 : 月清單_DGV.Columns(3).FillWeight = 60 : 月清單_DGV.Columns(4).FillWeight = 70
        月清單_DGV.Columns(5).FillWeight = 100 : 月清單_DGV.Columns(6).FillWeight = 200 : 月清單_DGV.Columns(7).FillWeight = 70 : 月清單_DGV.Columns(8).FillWeight = 80
        月清單_DGV.Columns(9).FillWeight = 70 : 月清單_DGV.Columns(10).FillWeight = 80 : 月清單_DGV.Columns(11).FillWeight = 50 : 月清單_DGV.Columns(12).Visible = False
        月清單_DGV.Columns(13).Visible = False
        For I As Integer = 0 To 月清單_DGV.Rows.Count - 1
            If 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期六" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期日" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            End If
        Next
    End Sub
    Private Sub 年份_下拉表單資料載入()
        SQL_年份() : 年份_cb.Items.Clear() : While (dr.Read()) : 年份_cb.Items.Add(dr("年份")) : End While : conn.Close()
    End Sub
    Private Sub 加班申請資料_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        剛開啟 = True : 年份_下拉表單資料載入() : 年份_cb.Text = Year(Today) : 月份_cb.SelectedIndex = Month(Today) - 1 : 審核人_tb.Text = gUserName : 請假類別_cb.SelectedIndex = 0
        Set_加班明細() : 剛開啟 = False
    End Sub
    Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 介面關閉_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        MyModule1.虛擬桌面開啟()
    End Sub
    Private Sub 月清單_DGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 月清單_DGV.CellClick
        If e.RowIndex = -1 Then : Else
            員工編號_tb.Text = 月清單_DGV("員工編號", e.RowIndex).Value.ToString : 申請人_tb.Text = 月清單_DGV("姓名", e.RowIndex).Value.ToString
            特假日期_tb.Text = 月清單_DGV("日期", e.RowIndex).Value.ToString : 審核人1_tb.Text = 月清單_DGV("核准人1", e.RowIndex).Value.ToString
            事由_tb.Text = 月清單_DGV("事由紀錄", e.RowIndex).Value.ToString : 審核人2_tb.Text = 月清單_DGV("核准人2", e.RowIndex).Value.ToString
            DTP1_tb.Text = 月清單_DGV("核准日期1", e.RowIndex).Value.ToString : DTP2_tb.Text = 月清單_DGV("核准日期2", e.RowIndex).Value.ToString
            If 審核人1_tb.Text = "" Then
                審核人1_tb.Text = gUserName : 審核人2_tb.Text = ""
            Else
                審核人2_tb.Text = gUserName
            End If
            '------------驗貨報告讀取--------------------------------------------------------------------------------------------
            If 月清單_DGV("圖片資料庫", e.RowIndex).Value.ToString <> "" And 月清單_DGV("圖片編號", e.RowIndex).Value.ToString <> "" Then
                圖片庫 = 月清單_DGV("圖片資料庫", e.RowIndex).Value.ToString : SQL_連線字串_圖片資料庫() : PA2 = 月清單_DGV("圖片編號", e.RowIndex).Value.ToString
                SQL_加班圖() : PictureBox1.Image = Nothing
                While dr.Read() = True
                    Dim unused As Byte() = New Byte(-1) {} : Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                    Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                End While : conn.Close() : PictureBox1.SizeMode = 4
            Else
                PictureBox1.Image = Nothing
            End If
        End If
    End Sub
    Private Sub 月清單_DGV_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 月清單_DGV.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 月清單_DGV.Rows.Count - 1 Then : Exit Sub : Else
            If Strings.Left(月清單_DGV("姓名", e.RowIndex).Value.ToString, 10) <> Strings.Left(月清單_DGV("姓名", e.RowIndex + 1).Value.ToString, 10) Then
                Dim startX As Integer = IIf(月清單_DGV.RowHeadersVisible, 月清單_DGV.RowHeadersWidth, 0) : Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 月清單_DGV.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 月清單_DGV.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY) : Exit Sub
            End If
        End If
        For I As Integer = 0 To 月清單_DGV.Rows.Count - 1
            If 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期六" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期日" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            End If
        Next
    End Sub
    Private Sub 年份_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 年份_cb.SelectedIndexChanged
        If 剛開啟 = False Then : Set_加班明細() : End If
    End Sub
    Private Sub 月份_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 月份_cb.SelectedIndexChanged
        If 剛開啟 = False Then : Set_加班明細() : End If
    End Sub
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        圖片傳遞 = PictureBox1.Image : 圖片放大視窗.ShowDialog()
    End Sub
    Private Sub 列印_bt_Click(sender As Object, e As EventArgs) Handles 列印_bt.Click
        xlApp = CType(CreateObject("Excel.Application"), Application)
        xlBook = xlApp.Workbooks.Add
        xlApp.DisplayAlerts = True
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized

        xlSheet = NewMethod(xlBook) : xlApp.Sheets(1).Select
        xlApp.Sheets(1).Name = "加班申請資料" : xlBook.Activate() : xlSheet.Activate()

        xlSheet.Cells(1, 1) = "黃柏翰皮膚科診所加班申請資料" : xlSheet.Cells(2, 1) = CInt(年份_cb.Text) - 1911 & "年" & 月份_cb.Text & "月 - 全月"
        xlSheet.Cells(3, 1) = "項" : xlSheet.Cells(3, 2) = "姓名" : xlSheet.Cells(3, 3) = "日期" : xlSheet.Cells(3, 4) = "星期" : xlSheet.Cells(3, 5) = "加班狀態" : xlSheet.Cells(3, 6) = "事由紀錄"
        For i As Integer = 0 To 月清單_DGV.Rows.Count - 1
            xlSheet.Cells(4 + i, 1) = i + 1 : xlSheet.Cells(4 + i, 2) = 月清單_DGV("姓名", i).Value.ToString
            xlSheet.Cells(4 + i, 3) = 月清單_DGV("日期", i).Value.ToString : xlSheet.Cells(4 + i, 4) = 月清單_DGV("星期", i).Value.ToString
            xlSheet.Cells(4 + i, 5) = 月清單_DGV("加班狀態", i).Value.ToString : xlSheet.Cells(4 + i, 6) = 月清單_DGV("事由紀錄", i).Value.ToString
        Next : 位置1 = 4 + 月清單_DGV.Rows.Count - 1 : AA(xlApp, xlSheet)
        xlSheet.PageSetup.PrintArea = "" : xlApp.Cells.Select() : xlSheet.Range("B1").Select() : xlApp.Application.WindowState = xlMinimized : MsgBox("列印完成")
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    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 = "微軟正黑體" : .Size = 12 : .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 = 4
        xlSheet.Columns("A:F").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.Range("F4:F" & 位置1).Select()

        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
        xlSheet.Columns("B:F").Select : myExcel.Columns("B:F").EntireColumn.AutoFit
        xlSheet.Rows("4:4").Select : myExcel.ActiveWindow.FreezePanes = True
        xlSheet.Range("B3:F3").Select() : myExcel.Selection.AutoFilter
        xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 35
        With myExcel.Selection : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With
        xlSheet.Range("A3:F" & 位置1).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
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        xlSheet.Range("A2:F2").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 : .Name = "微軟正黑體" : .Size = 12 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False
            .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
        myExcel.Selection.Font.Bold = True
        xlSheet.Range("A1:F1").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 : .Name = "微軟正黑體" : .Size = 16 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False
            .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With
        myExcel.Selection.Font.Bold = True
        xlSheet.Range("A3:F3").Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent1 : .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0 : End With
        xlSheet.Range("F4").Select()
        myExcel.ActiveWindow.DisplayGridlines = False
    End Sub
End Class