Option Strict Off
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
Public Class 財務_收支單一覽表
    ReadOnly ds, ds1, ds2, ds3, ds4 As New DataSet
    Dim DTP As String : Dim N1 As Integer : Dim N2 As Integer
    Dim xlApp As Application
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    Private Sub Set_清單()
        日期彙總_dgv.DataSource = Nothing : ds3.Clear()
        日期彙總_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        日期彙總_dgv.ColumnHeadersHeight = 25
        日期彙總_dgv.AllowUserToAddRows = False
        日期彙總_dgv.RowTemplate.Height = 25
        SQL_營運成本收支單一覽表_查詢清單純日期() : da.Fill(ds3) : 日期彙總_dgv.DataSource = ds3.Tables(0) : conn.Close()
        單據彙總_dgv.Columns(0).FillWeight = 100
    End Sub
    Private Sub Set_清單1()
        單據彙總_dgv.DataSource = Nothing : ds1.Clear()
        單據彙總_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        單據彙總_dgv.ColumnHeadersHeight = 25
        單據彙總_dgv.AllowUserToAddRows = False
        SQL_營運成本收支單一覽表_查詢清單() : da.Fill(ds1) : 單據彙總_dgv.DataSource = ds1.Tables(0) : conn.Close()
        單據彙總_dgv.RowTemplate.Height = 25
        單據彙總_dgv.Columns(0).FillWeight = 160 : 單據彙總_dgv.Columns(1).Visible = False : 單據彙總_dgv.Columns(2).FillWeight = 100 : 單據彙總_dgv.Columns(3).Visible = False
        單據彙總_dgv.Columns(4).FillWeight = 100 : 單據彙總_dgv.Columns(5).Visible = False : 單據彙總_dgv.Columns(6).Visible = False
    End Sub
    Private Sub Set_清單2()
        財務彙總_dgv.DataSource = Nothing : ds2.Clear()
        財務彙總_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        財務彙總_dgv.ColumnHeadersHeight = 25
        財務彙總_dgv.AllowUserToAddRows = False
        財務彙總_dgv.RowTemplate.Height = 30
        If 全部_ch.Checked = True And 含稅_ch.Checked = False And 非稅_ch.Checked = False Then
            SQL2 = ""
        ElseIf 全部_ch.Checked = False And 含稅_ch.Checked = True And 非稅_ch.Checked = False Then
            SQL2 = " AND (公帳流水帳.含稅 = 1)"
        ElseIf 全部_ch.Checked = False And 含稅_ch.Checked = False And 非稅_ch.Checked = True Then
            SQL2 = " AND (公帳流水帳.含稅 = 0 OR 公帳流水帳.含稅 IS NULL)"
        End If
        SQL_營運成本收支單一覽表_查詢清單2() : da.Fill(ds2) : 財務彙總_dgv.DataSource = ds2.Tables(0) : conn.Close()
        財務彙總_dgv.Columns(0).Visible = False : 財務彙總_dgv.Columns(1).FillWeight = 140 : 財務彙總_dgv.Columns(2).FillWeight = 140 : 財務彙總_dgv.Columns(3).FillWeight = 140
        財務彙總_dgv.Columns(4).FillWeight = 100 : 財務彙總_dgv.Columns(5).FillWeight = 100 : 財務彙總_dgv.Columns(6).FillWeight = 100 : 財務彙總_dgv.Columns(7).FillWeight = 100
        財務彙總_dgv.Columns(8).FillWeight = 100 : 財務彙總_dgv.Columns(9).FillWeight = 100
        If 彙總匯率_ch.Checked = False Then
            財務彙總_dgv.Columns(8).Visible = False : 財務彙總_dgv.Columns(9).Visible = False
        Else
            財務彙總_dgv.Columns(8).Visible = True : 財務彙總_dgv.Columns(9).Visible = True
        End If
        財務彙總_dgv.Columns(6).DefaultCellStyle.Format = "#,##0" : 財務彙總_dgv.Columns(7).DefaultCellStyle.Format = "#,##0" : 財務彙總_dgv.Columns(4).DefaultCellStyle.Format = "#,##0"
        財務彙總_dgv.Columns(5).DefaultCellStyle.Format = "#,##0"
        財務彙總_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        財務彙總_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        財務彙總_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        財務彙總_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        財務彙總_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
    End Sub
    Private Sub Set_清單3()
        財務彙總_天_dgv.DataSource = Nothing : ds4.Clear()
        財務彙總_天_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        財務彙總_天_dgv.ColumnHeadersHeight = 25
        財務彙總_天_dgv.AllowUserToAddRows = False
        財務彙總_天_dgv.RowTemplate.Height = 30

        If 全部_ch.Checked = True And 含稅_ch.Checked = False And 非稅_ch.Checked = False Then
            SQL2 = ""
        ElseIf 全部_ch.Checked = False And 含稅_ch.Checked = True And 非稅_ch.Checked = False Then
            SQL2 = " AND (公帳流水帳.含稅 = 1)"
        ElseIf 全部_ch.Checked = False And 含稅_ch.Checked = False And 非稅_ch.Checked = True Then
            SQL2 = " AND (公帳流水帳.含稅 = 0 OR 公帳流水帳.含稅 IS NULL)"
        End If
        SQL_營運成本收支單一覽表_查詢清單3() : da.Fill(ds4) : 財務彙總_天_dgv.DataSource = ds4.Tables(0) : conn.Close()
        財務彙總_天_dgv.Columns(0).Visible = False : 財務彙總_天_dgv.Columns(1).FillWeight = 140 : 財務彙總_天_dgv.Columns(2).FillWeight = 140 : 財務彙總_天_dgv.Columns(3).FillWeight = 140
        財務彙總_天_dgv.Columns(4).FillWeight = 100 : 財務彙總_天_dgv.Columns(5).FillWeight = 100 : 財務彙總_天_dgv.Columns(6).FillWeight = 100 : 財務彙總_天_dgv.Columns(7).FillWeight = 100
        財務彙總_天_dgv.Columns(8).FillWeight = 100 : 財務彙總_天_dgv.Columns(9).FillWeight = 100
        If 彙總匯率_ch.Checked = False Then
            財務彙總_天_dgv.Columns(8).Visible = False : 財務彙總_天_dgv.Columns(9).Visible = False
        Else
            財務彙總_天_dgv.Columns(8).Visible = True : 財務彙總_天_dgv.Columns(9).Visible = True
        End If
        財務彙總_天_dgv.Columns(6).DefaultCellStyle.Format = "#,##0" : 財務彙總_天_dgv.Columns(7).DefaultCellStyle.Format = "#,##0" : 財務彙總_天_dgv.Columns(4).DefaultCellStyle.Format = "#,##0"
        財務彙總_天_dgv.Columns(5).DefaultCellStyle.Format = "#,##0"
        財務彙總_天_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        財務彙總_天_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        財務彙總_天_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        財務彙總_天_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        財務彙總_天_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        財務彙總_天_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
    End Sub
    Private Sub Set_費用清單()
        明細_dgv.DataSource = Nothing : ds.Clear()
        明細_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        明細_dgv.ColumnHeadersHeight = 25
        明細_dgv.AllowUserToAddRows = False
        明細_dgv.RowTemplate.Height = 30
        If 全部_ch.Checked = True And 含稅_ch.Checked = False And 非稅_ch.Checked = False Then
            SQL2 = ""
        ElseIf 全部_ch.Checked = False And 含稅_ch.Checked = True And 非稅_ch.Checked = False Then
            SQL2 = " AND (公帳流水帳.含稅 = 1)"
        ElseIf 全部_ch.Checked = False And 含稅_ch.Checked = False And 非稅_ch.Checked = True Then
            SQL2 = " AND (公帳流水帳.含稅 = 0 OR 公帳流水帳.含稅 IS NULL)"
        End If
        SQL_營運成本收支單一覽表_查詢費用清單() : da.Fill(ds) : 明細_dgv.DataSource = ds.Tables(0) : conn.Close()
        明細_dgv.Columns(0).FillWeight = 250 : 明細_dgv.Columns(1).Visible = False : 明細_dgv.Columns(2).FillWeight = 250 : 明細_dgv.Columns(3).FillWeight = 400
        明細_dgv.Columns(4).FillWeight = 140 : 明細_dgv.Columns(5).FillWeight = 140 : 明細_dgv.Columns(6).FillWeight = 140 : 明細_dgv.Columns(7).FillWeight = 140
        明細_dgv.Columns(8).FillWeight = 140 : 明細_dgv.Columns(9).FillWeight = 140 : 明細_dgv.Columns(10).FillWeight = 140 : 明細_dgv.Columns(11).Visible = False
        明細_dgv.Columns(12).Visible = False
        If 彙總匯率_ch.Checked = False Then
            明細_dgv.Columns(9).Visible = False : 明細_dgv.Columns(10).Visible = False
        Else
            明細_dgv.Columns(9).Visible = True : 明細_dgv.Columns(10).Visible = True
        End If
        明細_dgv.Columns(1).ReadOnly = True : 明細_dgv.Columns(2).ReadOnly = True : 明細_dgv.Columns(9).ReadOnly = True : 明細_dgv.Columns(10).ReadOnly = True
        明細_dgv.Columns(4).DefaultCellStyle.Format = "#,##0" : 明細_dgv.Columns(5).DefaultCellStyle.Format = "#,##0" : 明細_dgv.Columns(6).DefaultCellStyle.Format = "#,##0"
        明細_dgv.Columns(7).DefaultCellStyle.Format = "#,##0"
        明細_dgv.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        台幣現金_tb.Text = "0" : 台幣支出_tb.Text = "0" : 台幣現金餘額_tb.Text = "0" : 台幣零用金_tb.Text = "0" : 台幣支票_tb.Text = "0"
        人民幣零用金_tb.Text = "0" : 人民幣支票_tb.Text = "0" : 人民幣現金_tb.Text = "0" : 人民幣支出_tb.Text = "0" : 人民幣現金餘額_tb.Text = "0"
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            If 明細_dgv.Rows(i).Cells("幣別").Value = "台幣現金" Then
                台幣現金_tb.Text = Val(台幣現金_tb.Text) + 明細_dgv.Rows(i).Cells("收入").Value
                台幣支出_tb.Text = Val(台幣支出_tb.Text) + 明細_dgv.Rows(i).Cells("支出").Value
                台幣零用金_tb.Text = Val(台幣零用金_tb.Text) + 明細_dgv.Rows(i).Cells("零用金").Value
                台幣支票_tb.Text = Val(台幣支票_tb.Text) + 明細_dgv.Rows(i).Cells("支票").Value
            End If
            If 明細_dgv.Rows(i).Cells("幣別").Value = "人民幣現金" Then
                人民幣現金_tb.Text = Val(人民幣現金_tb.Text) + 明細_dgv.Rows(i).Cells("收入").Value
                人民幣支出_tb.Text = Val(人民幣支出_tb.Text) + 明細_dgv.Rows(i).Cells("支出").Value

                人民幣零用金_tb.Text = Val(人民幣零用金_tb.Text) + 明細_dgv.Rows(i).Cells("零用金").Value
                人民幣支票_tb.Text = Val(人民幣支票_tb.Text) + 明細_dgv.Rows(i).Cells("支票").Value
            End If
        Next i
        台幣現金餘額_tb.Text = Val(台幣零用金_tb.Text) + Val(台幣現金_tb.Text) - Val(台幣支出_tb.Text)
        人民幣現金餘額_tb.Text = Val(人民幣零用金_tb.Text) + Val(人民幣現金_tb.Text) - Val(人民幣支出_tb.Text)
        台幣現金_tb.Text = Strings.Format(Val(台幣現金_tb.Text), "#,##0") : 台幣支出_tb.Text = Strings.Format(Val(台幣支出_tb.Text), "#,##0")
        台幣現金餘額_tb.Text = Strings.Format(Val(台幣現金餘額_tb.Text), "#,##0")
        台幣零用金_tb.Text = Strings.Format(Val(台幣零用金_tb.Text), "#,##0") : 台幣支票_tb.Text = Strings.Format(Val(台幣支票_tb.Text), "#,##0")
        人民幣零用金_tb.Text = Strings.Format(Val(人民幣零用金_tb.Text), "#,##0") : 人民幣支票_tb.Text = Strings.Format(Val(人民幣支票_tb.Text), "#,##0")
        人民幣現金_tb.Text = Strings.Format(Val(人民幣現金_tb.Text), "#,##0")
        人民幣支出_tb.Text = Strings.Format(Val(人民幣支出_tb.Text), "#,##0") : 人民幣現金餘額_tb.Text = Strings.Format(Val(人民幣現金餘額_tb.Text), "#,##0")
    End Sub
    Private Sub 日期格式()
        If 會計月_ch.Checked = True Then
            '-------------日期一----------------------------------------------------------------------------------------
            DTP = Strings.Left(Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd"), 8) & "01" : 開始日期_dtp.Value = DTP
            開始日期_dtp.Value = 開始日期_dtp.Value.AddMonths(-1)
            DTP = Strings.Left(Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd"), 8) & "26" : 開始日期_dtp.Value = DTP
            '-------------日期二----------------------------------------------------------------------------------------
            DTP = Strings.Left(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 8) & "25" : 結束日期_dtp.Value = DTP
        Else
            '-------------日期一----------------------------------------------------------------------------------------
            DTP = Strings.Left(Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd"), 8) & "01" : 開始日期_dtp.Value = DTP
            '-------------日期二----------------------------------------------------------------------------------------
            DTP = Strings.Left(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 8) & "01" : 結束日期_dtp.Value = DTP
            結束日期_dtp.Value = 結束日期_dtp.Value.AddMonths(1) : 結束日期_dtp.Value = 結束日期_dtp.Value.AddDays(-1)
        End If
    End Sub
    Private Sub 營運成本收支單一覽表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YD_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        DTP = Strings.Left(Strings.Format(Today(), "yyyy/MM/dd"), 8) & "01"
        開始日期_dtp.Value = DTP : 結束日期_dtp.Value = Today()
        日期格式() : 明細_rb.Checked = True : 財務彙總_天_dgv.Visible = False
    End Sub
    Dim 已超出 As Boolean
    Private Sub 財務彙總_dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 財務彙總_dgv.MouseUp
        'If 財務彙總_dgv.SelectedCells.Count = 1 Then : MsgBox(財務彙總_dgv.SelectedCells(0).ColumnIndex & "  " & 財務彙總_dgv.SelectedCells(0).RowIndex) : End If
        If 財務彙總_dgv.SelectedCells.Count = 1 Then : 已超出 = False : End If
        If 財務彙總_dgv.SelectedCells.Count > 0 Then
            If 財務彙總_dgv.SelectedCells(0).ColumnIndex >= 4 And 財務彙總_dgv.SelectedCells(0).ColumnIndex <= 7 Then
                If 已超出 = False Then
                    Dim x As Double = 0
                    For i As Integer = 0 To 財務彙總_dgv.SelectedCells.Count - 1
                        x += Val(財務彙總_dgv.Rows(財務彙總_dgv.SelectedCells.Item(i).RowIndex).Cells(財務彙總_dgv.SelectedCells.Item(i).ColumnIndex).Value)
                    Next i : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(x, "#,##0")
                Else : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0") : End If
            Else : 已超出 = True : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0")
            End If
        End If
    End Sub
    Private Sub 明細_dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 明細_dgv.MouseUp
        'If 明細_dgv.SelectedCells.Count = 1 Then : MsgBox(明細_dgv.SelectedCells(0).ColumnIndex & "  " & 明細_dgv.SelectedCells(0).RowIndex) : End If
        If 明細_dgv.SelectedCells.Count = 1 Then : 已超出 = False : End If
        If 明細_dgv.SelectedCells.Count > 0 Then
            If 明細_dgv.SelectedCells(0).ColumnIndex >= 4 And 明細_dgv.SelectedCells(0).ColumnIndex <= 7 Then
                If 已超出 = False Then
                    Dim x As Double = 0
                    For i As Integer = 0 To 明細_dgv.SelectedCells.Count - 1
                        x += Val(明細_dgv.Rows(明細_dgv.SelectedCells.Item(i).RowIndex).Cells(明細_dgv.SelectedCells.Item(i).ColumnIndex).Value)
                    Next i : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(x, "#,##0")
                Else : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0") : End If
            Else : 已超出 = True : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0")
            End If
        End If
    End Sub
    Private Sub 單據彙總_dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 單據彙總_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            收支單號_tb.Text = 單據彙總_dgv(0, e.RowIndex).Value.ToString : 驗證碼_tb.Text = 單據彙總_dgv(6, e.RowIndex).Value.ToString
            申請人_tb.Text = 單據彙總_dgv(1, e.RowIndex).Value.ToString : 申請日期_dtp.Value = 單據彙總_dgv(2, e.RowIndex).Value.ToString
            審核人_tb.Text = 單據彙總_dgv(3, e.RowIndex).Value.ToString : 圖片編碼_tb.Text = 單據彙總_dgv(5, e.RowIndex).Value.ToString
            If 單據彙總_dgv(4, e.RowIndex).Value.ToString = "" Then
                審核日期_dtp.Value = 單據彙總_dgv(2, e.RowIndex).Value.ToString
            Else
                審核日期_dtp.Value = 單據彙總_dgv(4, e.RowIndex).Value.ToString
            End If
            Set_費用清單()
        End If
    End Sub
    Private Sub 明細_dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 明細_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            Dim NV1 As Integer
            收支單號_tb.Text = 明細_dgv(0, e.RowIndex).Value.ToString
            For i As Integer = 0 To 單據彙總_dgv.Rows.Count - 1
                If 收支單號_tb.Text = 單據彙總_dgv(0, i).Value.ToString Then : NV1 = i : Exit For : End If
            Next
            收支單號_tb.Text = 單據彙總_dgv(0, NV1).Value.ToString : 驗證碼_tb.Text = 單據彙總_dgv(6, NV1).Value.ToString
            申請人_tb.Text = 單據彙總_dgv(1, NV1).Value.ToString : 申請日期_dtp.Value = 單據彙總_dgv(2, NV1).Value.ToString
            審核人_tb.Text = 單據彙總_dgv(3, NV1).Value.ToString : 圖片編碼_tb.Text = 單據彙總_dgv(5, NV1).Value.ToString
            If 單據彙總_dgv(4, NV1).Value.ToString = "" Then
                審核日期_dtp.Value = 單據彙總_dgv(2, NV1).Value.ToString
            Else
                審核日期_dtp.Value = 單據彙總_dgv(4, NV1).Value.ToString
            End If
            Set_費用清單()
        End If
    End Sub
    Private Sub 日期彙總_dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 日期彙總_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            If 日期浮動_cb.Checked = True Then
                結束日期_dtp.Value = 日期彙總_dgv(0, e.RowIndex).Value.ToString
            End If
            申請日期_dtp.Value = 日期彙總_dgv(0, e.RowIndex).Value.ToString : Set_費用清單() : Set_清單3() : Set_清單2()
            If 單據彙總_dgv.Rows.Count > 0 Then
                圖片編碼_tb.Text = 單據彙總_dgv(5, 0).Value.ToString
            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) : Dim linePen1 As New Pen(Color.Red, 2) : Dim linePen2 As New Pen(Color.Green, 2)
        If e.RowIndex = 單據彙總_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 單據彙總_dgv(2, e.RowIndex).Value.ToString <> 單據彙總_dgv(2, e.RowIndex + 1).Value.ToString 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
    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) : Dim linePen1 As New Pen(Color.Red, 2) : Dim linePen2 As New Pen(Color.Green, 2)
        If e.RowIndex = 財務彙總_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 財務彙總_dgv(0, e.RowIndex).Value.ToString <> 財務彙總_dgv(0, e.RowIndex + 1).Value.ToString 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
            If 財務彙總_dgv(8, e.RowIndex).Value.ToString <> 財務彙總_dgv(8, e.RowIndex + 1).Value.ToString 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(linePen2, startX, startY, endX, startY)
                Exit Sub
            End If
            If 財務彙總_dgv(1, e.RowIndex).Value.ToString <> 財務彙總_dgv(1, e.RowIndex + 1).Value.ToString 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(linePen1, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub 日期浮動_cb_CheckedChanged(sender As Object, e As EventArgs) Handles 日期浮動_cb.Click
        If 日期浮動_cb.Checked = False Then
            日期浮動_cb.Checked = False : 當月_bt.PerformClick()
        End If
    End Sub
    Private Sub 全部_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 全部_ch.Click
        全部_ch.Checked = True : 含稅_ch.Checked = False : 非稅_ch.Checked = False : Set_費用清單() : Set_清單() : Set_清單2() : 彙總計算()
    End Sub
    Private Sub 含稅_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 含稅_ch.Click
        全部_ch.Checked = False : 含稅_ch.Checked = True : 非稅_ch.Checked = False : Set_費用清單() : Set_清單() : Set_清單2() : 彙總計算()
    End Sub
    Private Sub 非稅_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 非稅_ch.Click
        全部_ch.Checked = False : 含稅_ch.Checked = False : 非稅_ch.Checked = True : Set_費用清單() : Set_清單() : Set_清單2() : 彙總計算()
    End Sub
    Private Sub 當日_bt_Click(sender As Object, e As EventArgs) Handles 當日_bt.Click
        日期浮動_cb.Checked = False : DTP = Strings.Format(Today(), "yyyy/MM/dd")
        開始日期_dtp.Value = DTP : 結束日期_dtp.Value = DTP
    End Sub
    Private Sub 當月_bt_Click(sender As Object, e As EventArgs) Handles 當月_bt.Click
        日期浮動_cb.Checked = False : DTP = Strings.Left(Strings.Format(Today(), "yyyy/MM/dd"), 8) & "01"
        開始日期_dtp.Value = DTP : 結束日期_dtp.Value = Today()
    End Sub
    Private Sub 彙總計算()
        If 明細_rb.Checked = False And 彙總_rb.Checked = True Then
            台幣現金_tb.Text = "0" : 台幣支出_tb.Text = "0" : 台幣現金餘額_tb.Text = "0" : 台幣零用金_tb.Text = "0" : 台幣支票_tb.Text = "0"
            人民幣零用金_tb.Text = "0" : 人民幣支票_tb.Text = "0" : 人民幣現金_tb.Text = "0" : 人民幣支出_tb.Text = "0" : 人民幣現金餘額_tb.Text = "0"
            For i As Integer = 0 To 財務彙總_dgv.Rows.Count - 1
                If 財務彙總_dgv.Rows(i).Cells("幣別").Value = "台幣現金" Then
                    台幣現金_tb.Text = Val(台幣現金_tb.Text) + 財務彙總_dgv.Rows(i).Cells("收入").Value
                    台幣支出_tb.Text = Val(台幣支出_tb.Text) + 財務彙總_dgv.Rows(i).Cells("支出").Value
                    台幣零用金_tb.Text = Val(台幣零用金_tb.Text) + 財務彙總_dgv.Rows(i).Cells("零用金").Value
                    台幣支票_tb.Text = Val(台幣支票_tb.Text) + 財務彙總_dgv.Rows(i).Cells("支票").Value
                End If
                If 財務彙總_dgv.Rows(i).Cells("幣別").Value = "人民幣現金" Then
                    If 財務彙總_dgv.Rows(i).Cells("主項").Value <> "上月結存" Then
                        人民幣現金_tb.Text = Val(人民幣現金_tb.Text) + 財務彙總_dgv.Rows(i).Cells("收入").Value
                        人民幣支出_tb.Text = Val(人民幣支出_tb.Text) + 財務彙總_dgv.Rows(i).Cells("支出").Value
                        人民幣零用金_tb.Text = Val(人民幣零用金_tb.Text) + 財務彙總_dgv.Rows(i).Cells("零用金").Value
                        人民幣支票_tb.Text = Val(人民幣支票_tb.Text) + 財務彙總_dgv.Rows(i).Cells("支票").Value
                    End If
                End If
            Next i
            台幣現金餘額_tb.Text = Val(台幣零用金_tb.Text) + Val(台幣現金_tb.Text) - Val(台幣支出_tb.Text)
            人民幣現金餘額_tb.Text = Val(人民幣零用金_tb.Text) + Val(人民幣現金_tb.Text) - Val(人民幣支出_tb.Text)
            台幣現金_tb.Text = Strings.Format(Val(台幣現金_tb.Text), "#,##0") : 台幣支出_tb.Text = Strings.Format(Val(台幣支出_tb.Text), "#,##0")
            台幣現金餘額_tb.Text = Strings.Format(Val(台幣現金餘額_tb.Text), "#,##0")
            台幣零用金_tb.Text = Strings.Format(Val(台幣零用金_tb.Text), "#,##0") : 台幣支票_tb.Text = Strings.Format(Val(台幣支票_tb.Text), "#,##0")
            人民幣零用金_tb.Text = Strings.Format(Val(人民幣零用金_tb.Text), "#,##0") : 人民幣支票_tb.Text = Strings.Format(Val(人民幣支票_tb.Text), "#,##0")
            人民幣現金_tb.Text = Strings.Format(Val(人民幣現金_tb.Text), "#,##0")
            人民幣支出_tb.Text = Strings.Format(Val(人民幣支出_tb.Text), "#,##0") : 人民幣現金餘額_tb.Text = Strings.Format(Val(人民幣現金餘額_tb.Text), "#,##0")
        End If
    End Sub
    Private Sub 上個月_bt_Click(sender As Object, e As EventArgs) Handles 上個月_bt.Click
        日期浮動_cb.Checked = False
        If 會計月_ch.Checked = True Then
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(-1)
            開始日期_dtp.Value = 中間日期_dtp.Value
            結束日期_dtp.Value = 中間日期_dtp.Value
        Else
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(-1)
            開始日期_dtp.Value = 中間日期_dtp.Value
            結束日期_dtp.Value = 中間日期_dtp.Value
        End If : 日期格式()
    End Sub
    Private Sub 下個月_bt_Click(sender As Object, e As EventArgs) Handles 下個月_bt.Click
        日期浮動_cb.Checked = False
        If 會計月_ch.Checked = True Then
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(1)
            開始日期_dtp.Value = 中間日期_dtp.Value
            結束日期_dtp.Value = 中間日期_dtp.Value
        Else
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(1)
            開始日期_dtp.Value = 中間日期_dtp.Value
            結束日期_dtp.Value = 中間日期_dtp.Value
        End If : 日期格式()
    End Sub
    Private Sub 開始日期_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 開始日期_dtp.ValueChanged
        If 日期浮動_cb.Checked = True Then
            Set_清單1() : Set_清單2() : 彙總計算()
        Else
            Set_清單1() : Set_清單() : Set_清單2() : 彙總計算()
        End If
    End Sub
    Private Sub 結束日期_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 結束日期_dtp.ValueChanged
        If 日期浮動_cb.Checked = True Then
            Set_清單1() : Set_清單2() : 彙總計算()
        Else
            Set_清單1() : Set_清單() : Set_清單2() : 彙總計算()
        End If
    End Sub
    Private Sub 會計月_CheckedChanged(sender As Object, e As EventArgs) Handles 會計月_ch.Click
        會計月_ch.Checked = True : 一般月_ch.Checked = False : 日期格式()
    End Sub
    Private Sub 一般月_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 一般月_ch.Click
        會計月_ch.Checked = False : 一般月_ch.Checked = True : 日期格式()
    End Sub
    Private Sub 明細_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 明細_rb.Click
        明細_dgv.Visible = True : 財務彙總_天_dgv.Visible = False
        明細_rb.Checked = True : 彙總_rb.Checked = False
        台幣現金_tb.Text = "0" : 台幣支出_tb.Text = "0" : 台幣現金餘額_tb.Text = "0" : 台幣零用金_tb.Text = "0" : 台幣支票_tb.Text = "0" ': 單據彙總_dgv.Enabled = True
    End Sub
    Private Sub 彙總_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 彙總_rb.Click
        明細_dgv.Visible = False : 財務彙總_天_dgv.Visible = True
        明細_rb.Checked = False : 彙總_rb.Checked = True
        收支單號_tb.Text = "" : 申請人_tb.Text = "" : 審核人_tb.Text = "" : 明細_dgv.DataSource = Nothing : ds.Clear() ': 單據彙總_dgv.Enabled = False
        彙總計算()
    End Sub
    Private Sub 圖片編碼_tb_TextChanged(sender As Object, e As EventArgs) Handles 圖片編碼_tb.TextChanged
        SQL_營運成本收支單一覽表_查詢簽名檔圖片()
        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)
            簽名圖_pb.Image = Bitmap.FromStream(oStream)
        End While
        conn.Close()
        簽名圖_pb.SizeMode = 4
    End Sub
    Private Sub 彙總匯率_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 彙總匯率_ch.Click
        Set_清單() : Set_清單2()
    End Sub
    Private Sub 支付單列印成EXCEL_Click(sender As Object, e As EventArgs) Handles 支付單列印成EXCEL.Click
        xlApp = CType(CreateObject("Excel.Application"), Application)
        xlBook = xlApp.Workbooks.Add
        xlApp.DisplayAlerts = True
        xlBook.Activate()
        xlSheet = NewMethod(xlBook)
        xlSheet.Activate()
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized

        xlSheet.Cells(1, 1) = "年度" : xlSheet.Cells(1, 2) = Year(Today) - 1911
        If 明細_rb.Checked = True And 彙總_rb.Checked = False Then
            xlSheet.Cells(1, 3) = "雅邸家居-營運費用收支明細表"
            xlSheet.Cells(2, 1) = "收款日期" : xlSheet.Cells(2, 2) = "會計科目" : xlSheet.Cells(2, 3) = "客戶或用途"
            xlSheet.Cells(2, 4) = "零用金" : xlSheet.Cells(2, 5) = "支票" : xlSheet.Cells(2, 6) = "收入" : xlSheet.Cells(2, 7) = "支出" : xlSheet.Cells(2, 8) = "現金餘額" : xlSheet.Cells(2, 9) = "票期"

            For i As Integer = 0 To 明細_dgv.Rows.Count - 1
                xlSheet.Cells(i + 3, 1) = 申請日期_dtp.Value : xlSheet.Cells(i + 3, 2) = 明細_dgv.Rows(i).Cells("會計科目").Value
                xlSheet.Cells(i + 3, 3) = 明細_dgv.Rows(i).Cells("用途").Value : xlSheet.Cells(i + 3, 4) = 明細_dgv.Rows(i).Cells("零用金").Value
                xlSheet.Cells(i + 3, 5) = 明細_dgv.Rows(i).Cells("支票").Value : xlSheet.Cells(i + 3, 6) = 明細_dgv.Rows(i).Cells("收入").Value
                xlSheet.Cells(i + 3, 7) = 明細_dgv.Rows(i).Cells("支出").Value : xlSheet.Cells(i + 3, 9) = 明細_dgv.Rows(i).Cells("票期").Value
                If i = 0 Then
                    xlSheet.Cells(i + 3, 8) = "=D3+F3-G3"
                Else
                    xlSheet.Cells(i + 3, 8) = "=H" & i + 2 & "+D" & i + 3 & "+F" & i + 3 & "-G" & i + 3 & ""
                End If
                With xlApp.Selection
                    .Font.Bold = True
                End With
                N1 = i + 3 : N2 = i + 3
            Next i
            xlSheet.Cells(N1 + 1, 3) = "以下空白" : xlSheet.Cells(N1 + 3, 1) = "簽核(點交)" : xlSheet.Cells(N1 + 3, 7) = "製表 : " & 申請人_tb.Text & " - " & 申請日期_dtp.Value
            xlSheet.Cells(N1 + 2, 4) = "=SUM(D3:D" & N1 + 1 & ")" : xlSheet.Cells(N1 + 2, 5) = "=SUM(E3:E" & N1 + 1 & ")" : xlSheet.Cells(N1 + 2, 6) = "=SUM(F3:F" & N1 + 1 & ")"
            xlSheet.Cells(N1 + 2, 7) = "=SUM(G3:G" & N1 + 1 & ")" : xlSheet.Cells(N1 + 2, 8) = "=H" & N1 : xlSheet.Cells(N1 + 2, 1) = "合計"

            AA(xlApp, xlSheet) : BB(xlApp, xlSheet)
        ElseIf 明細_rb.Checked = False And 彙總_rb.Checked = True Then
            xlSheet.Cells(1, 3) = "雅邸家居-營運費用收支彙總簡表 - " & Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd") & " 到 " & Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd")
            xlSheet.Cells(2, 1) = "日期" : xlSheet.Cells(2, 2) = "主項" : xlSheet.Cells(2, 3) = "子項" : xlSheet.Cells(2, 4) = "子細項" : xlSheet.Cells(2, 5) = "零用金"
            xlSheet.Cells(2, 6) = "支票" : xlSheet.Cells(2, 7) = "收入" : xlSheet.Cells(2, 8) = "支出" : xlSheet.Cells(2, 9) = "現金餘額"

            For i As Integer = 0 To 財務彙總_dgv.Rows.Count - 1
                xlSheet.Cells(i + 3, 1) = Strings.Left(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 7)
                xlSheet.Cells(i + 3, 2) = 財務彙總_dgv.Rows(i).Cells("主項").Value
                xlSheet.Cells(i + 3, 3) = 財務彙總_dgv.Rows(i).Cells("子項").Value
                xlSheet.Cells(i + 3, 4) = 財務彙總_dgv.Rows(i).Cells("子細項").Value
                xlSheet.Cells(i + 3, 5) = 財務彙總_dgv.Rows(i).Cells("零用金").Value
                xlSheet.Cells(i + 3, 6) = 財務彙總_dgv.Rows(i).Cells("支票").Value
                xlSheet.Cells(i + 3, 7) = 財務彙總_dgv.Rows(i).Cells("收入").Value
                xlSheet.Cells(i + 3, 8) = 財務彙總_dgv.Rows(i).Cells("支出").Value
                If i = 0 Then
                    xlSheet.Cells(i + 3, 9) = "=E3+G3-H3"
                Else
                    xlSheet.Cells(i + 3, 9) = "=I" & i + 2 & "+E" & i + 3 & "+G" & i + 3 & "-H" & i + 3 & ""
                End If
                With xlApp.Selection
                    .Font.Bold = True
                End With
                N1 = i + 3 : N2 = i + 3
            Next i
            xlSheet.Cells(N1 + 1, 2) = "以下空白" : xlSheet.Cells(N1 + 3, 1) = "簽核" : xlSheet.Cells(N1 + 3, 5) = "製表 : " & gUserName & " - " & Today()
            xlSheet.Cells(N1 + 2, 5) = "=SUM(E3:E" & N1 + 1 & ")" : xlSheet.Cells(N1 + 2, 6) = "=SUM(F3:F" & N1 + 1 & ")" : xlSheet.Cells(N1 + 2, 7) = "=SUM(G3:G" & N1 + 1 & ")"
            xlSheet.Cells(N1 + 2, 8) = "=SUM(H3:H" & N1 + 1 & ")" : xlSheet.Cells(N1 + 2, 9) = "=I" & N1 : xlSheet.Cells(N1 + 2, 1) = "合計"

            CC(xlApp, xlSheet)
        End If

        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        xlSheet.Cells.Select()
        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 Application, ByVal xlSheet As Worksheet)
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .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.NumberFormatLocal = "m/d;@"
        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("B1").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:B").Select : myExcel.Selection.ColumnWidth = 25
        xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 30
        xlSheet.Columns("D:H").Select : myExcel.Selection.ColumnWidth = 10 : myExcel.Selection.NumberFormatLocal = "#,##0;[紅色]-#,##0"
        xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 10 : myExcel.Selection.NumberFormatLocal = "[$-zh-TW]e/m/d;@"
        xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 25

        xlSheet.Range("A2:I2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With
        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("A2:I" & N1 + 2).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.ActiveWindow.DisplayGridlines = False

        xlSheet.Rows("3:" & N1 + 2).Select : myExcel.Selection.RowHeight = 20
        xlSheet.Rows(N1 + 3 & ":" & N1 + 3).Select : myExcel.Selection.RowHeight = 40

        xlSheet.Columns("I:I").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("A" & N1 + 2 & ":C" & N1 + 2).Select() : myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True
        End With

        xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 25
        With myExcel.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
        xlSheet.Columns("A:A").EntireColumn.AutoFit
        xlSheet.Columns("B:B").EntireColumn.AutoFit
        xlSheet.Columns("C:C").EntireColumn.AutoFit
    End Sub
    Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        Clipboard.SetDataObject(簽名圖_pb.Image)
        xlSheet.Range("B" & N2 + 3).Select()
        myExcel.ActiveSheet.Pictures.Paste.Select
        myExcel.Selection.ShapeRange.Height = 43
        myExcel.Selection.ShapeRange.IncrementTop(3)
    End Sub
    Private Sub CC(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .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 = 8
        xlSheet.Columns("B:C").Select : myExcel.Selection.ColumnWidth = 15
        xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 20
        xlSheet.Columns("E:I").Select : myExcel.Selection.ColumnWidth = 10 : myExcel.Selection.NumberFormatLocal = "#,##0;[紅色]-#,##0"
        xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 25
        xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 25
        xlSheet.Rows("3:" & N1 + 2).Select : myExcel.Selection.RowHeight = 20
        xlSheet.Rows(N1 + 3 & ":" & N1 + 3).Select : myExcel.Selection.RowHeight = 40

        xlSheet.Rows("2:2").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("A1").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("B1").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.Rows("1:1").Select
        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

        xlSheet.Range("A2:I" & N1 + 2).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.Range("A" & N1 + 2 & ":D" & N1 + 2).Select() : myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True
        End With

        xlSheet.Columns("E:I").Select : myExcel.Selection.NumberFormatLocal = "#,##0;[紅色]-#,##0"
        myExcel.ActiveWindow.DisplayGridlines = False
        xlSheet.Columns("B:B").EntireColumn.AutoFit
        xlSheet.Columns("D:D").EntireColumn.AutoFit
    End Sub
    '----------------------滑鼠右鍵--------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub 重新讀取ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 重新讀取ToolStripMenuItem.Click
        Set_清單1() : Set_清單() : Set_清單2() : 彙總計算()
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        單據彙總_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        財務彙總_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        明細_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
End Class