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
Imports Telerik.Windows.Documents.Fixed

Public Class 太陽能資訊表
    Dim 已開啟 As Boolean = False
    Dim xlApp As Application
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    Private Sub Set_清單()
        Dim ds1 As New DataSet
        太陽_dgv.DataSource = Nothing : ds1.Clear()
        太陽_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        太陽_dgv.ColumnHeadersHeight = 25 : 太陽_dgv.AllowUserToAddRows = False : 太陽_dgv.RowTemplate.Height = 25
        Dim 讀取條件2 As String = YS員工_cb.Text : If YS員工_cb.Text = "所有人" Then : 讀取條件2 = "%" : End If
        Dim 讀取條件 As String
        If 包含摘要資料_ch.Checked Then
            讀取條件 = "WHERE  (案件摘要_太陽.單據編號 LIKE N'獨-" & 讀取條件2 & "') OR  (案件摘要總表.製錶 LIKE N'" & 讀取條件2 & "')"
        Else
            讀取條件 = "WHERE  (案件摘要_太陽.單據編號 LIKE N'獨-" & 讀取條件2 & "') "
        End If
        'WHERE  (單據編號 LIKE N'" & 單據編號 & "')
        SQL_案件太陽讀取_改(讀取條件)
        da.Fill(ds1) : 太陽_dgv.DataSource = ds1.Tables(0) : conn.Close()
        For i As Integer = 0 To 太陽_dgv.Columns.Count - 1 : 太陽_dgv.Columns(i).Visible = False : Next
        太陽_dgv.Columns(1).Visible = True : 太陽_dgv.Columns(1).ReadOnly = True
        For i As Integer = 0 To 太陽_dgv.Rows.Count - 1 : 太陽_dgv.Rows(i).Cells(1).Value = "檔案-" & i + 1 : Next
    End Sub
    Private Sub 太陽能資訊表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        已開啟 = False
        SQL_客戶資料表製表下拉() : YS員工_cb.Items.Clear() : YS員工_cb.Items.Add("所有人") : While (dr.Read()) : YS員工_cb.Items.Add(dr("姓名")) : End While
        If CInt(登入人級別) <= 5 Then : YS員工_cb.Enabled = True : YS員工_cb.Text = "所有人" : Else : YS員工_cb.Enabled = False : YS員工_cb.Text = gUserName : End If
        饋線狀態_tb.Text = "" : 太_出租_tb.Text = "" : 太_出租_聯_tb.Text = "" : 太_承租_tb.Text = "" : 太_承租_聯_tb.Text = "" : KW數_tb.Text = "" : 太_租金比_tb.Text = ""
        並聯起組_tb.Text = "" : 包含摘要資料_ch.Checked = False : 地主_tb.Text = "" : 地址_tb.Text = ""
        太陽開始_dtp.Format = DateTimePickerFormat.Custom : 太陽開始_dtp.CustomFormat = " " : 太陽結束_dtp.Format = DateTimePickerFormat.Custom : 太陽結束_dtp.CustomFormat = " "
        Set_清單()
        已開啟 = True
    End Sub

    Private Sub 太陽_dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles 太陽_dgv.CellClick
        If e.RowIndex = -1 Then : Else : 太陽_dgv點擊(e.RowIndex) : End If
    End Sub
    Private Sub 太陽_dgv點擊(DGVX As Integer)
        太陽流水號_tb.Text = 太陽_dgv.Rows(DGVX).Cells("流水號").Value.ToString
        太_出租_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_出租方").Value.ToString : 太_承租_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_承租方").Value.ToString
        KW數_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_KW數").Value.ToString : 太_租金比_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_租金%").Value.ToString
        太_出租_聯_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_出租人").Value.ToString : 太_承租_聯_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_承租人").Value.ToString
        饋線狀態_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_饋線狀態").Value.ToString : 並聯起組_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_並聯起租").Value.ToString
        地主_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_地主").Value.ToString : 地址_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_地址").Value.ToString

        If 太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString = "" Then : 太陽開始_dtp.CustomFormat = " " : Else
            If Strings.Left(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 5) = "  -  " Then : 太陽開始_dtp.CustomFormat = " " : Else
                太陽開始_dtp.CustomFormat = "yyyy/MM/dd" : 太陽開始_dtp.Value = Strings.Left(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 10)
            End If
        End If
        If 太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString = "" Then : 太陽結束_dtp.CustomFormat = " " : Else
            If Strings.Right(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 5) = "  -  " Then : 太陽結束_dtp.CustomFormat = " " : Else
                太陽結束_dtp.CustomFormat = "yyyy/MM/dd" : 太陽結束_dtp.Value = Strings.Right(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 10)
            End If
        End If
    End Sub

    Private Sub 包含摘要資料_ch_Click(sender As Object, e As EventArgs) Handles 包含摘要資料_ch.Click
        If 已開啟 Then
            饋線狀態_tb.Text = "" : 太_出租_tb.Text = "" : 太_出租_聯_tb.Text = "" : 太_承租_tb.Text = "" : 太_承租_聯_tb.Text = "" : KW數_tb.Text = "" : 太_租金比_tb.Text = ""
            並聯起組_tb.Text = "" : 地主_tb.Text = "" : 地址_tb.Text = "" : 太陽流水號_tb.Text = ""
            太陽開始_dtp.Format = DateTimePickerFormat.Custom : 太陽開始_dtp.CustomFormat = " " : 太陽結束_dtp.Format = DateTimePickerFormat.Custom : 太陽結束_dtp.CustomFormat = " "
            Set_清單()
        End If
    End Sub

    Private Sub 太陽新增_bt_Click(sender As Object, e As EventArgs) Handles 太陽新增_bt.Click

        Dim NUM0 As Integer : DTP1 = Strings.Format(Today(), "yyyyMMdd")
        SQL_案件太陽流水() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
        NUM0 += 1
        If NUM0 < 10 Then : 太陽流水號_tb.Text = "SP-" & DTP1 & "-00" & NUM0
        ElseIf NUM0 > 9 And NUM0 < 100 Then : 太陽流水號_tb.Text = "SP-" & DTP1 & "-0" & NUM0
        ElseIf NUM0 > 99 Then : 太陽流水號_tb.Text = "SP-" & DTP1 & "-" & NUM0
        End If
        SQL_案件太陽清單新增_改("獨-" & gUserName, 太陽流水號_tb.Text, 太_出租_聯_tb.Text, 太_承租_聯_tb.Text, KW數_tb.Text, 太陽開始_dtp.Text & " - " & 太陽結束_dtp.Text,
                         太_租金比_tb.Text, 太_出租_tb.Text, 太_承租_tb.Text, 饋線狀態_tb.Text, 並聯起組_tb.Text, 地主_tb.Text, 地址_tb.Text)
        MsgBox("新增完畢!")
    End Sub

    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        If 太陽流水號_tb.Text = "" Then : MsgBox("請先選擇 [太陽能承租檔案] 或是 [新增太陽能承租檔案]，才能執行後續動作!!") : Else
            SQL_案件摘要太陽修改_改(太_出租_聯_tb.Text, 太_承租_聯_tb.Text, KW數_tb.Text, 太陽開始_dtp.Text & " - " & 太陽結束_dtp.Text, 太_租金比_tb.Text, 太_出租_tb.Text, 太_承租_tb.Text, 饋線狀態_tb.Text, 並聯起組_tb.Text, 太陽流水號_tb.Text, 地主_tb.Text, 地址_tb.Text)
            MsgBox("儲存完畢!") : Set_清單()
        End If
    End Sub

    Private Sub 太陽刪除_bt_Click(sender As Object, e As EventArgs) Handles 太陽刪除_bt.Click
        If 太陽流水號_tb.Text = "" Then : MsgBox("請先選擇 [太陽能承租檔案] 或是 [新增太陽能承租檔案]，才能執行後續動作!!") : Else
            Dim result As DialogResult = MessageBox.Show("請確定是否要刪除選定的太陽能承租資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result = DialogResult.Yes Then
                PA11 = InputBox("請輸入大寫 [YES]，並按下確認來刪除資料!!")
                If PA11 = "YES" Then : SQL_案件太陽刪除(太陽流水號_tb.Text) : Set_清單() : MsgBox("刪除完成!!") : End If
            End If
        End If
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub 列印_bt_Click(sender As Object, e As EventArgs) Handles 列印_bt.Click
        If 太陽流水號_tb.Text = "" Then
            MsgBox("沒有選擇太陽能資訊表!")
            太陽能資訊表_列印選項.單印_bt.Enabled = False
            太陽能資訊表_列印選項.ShowDialog() : 太陽能資訊表_列印選項.BringToFront()
            真列印()
        Else
            太陽能資訊表_列印選項.單印_bt.Enabled = True
            太陽能資訊表_列印選項.ShowDialog() : 太陽能資訊表_列印選項.BringToFront()
            真列印()
        End If
    End Sub
    Private Sub 真列印()
        If 太陽能列印選項 = "單印" Then
            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(2, 2) = "太陽能資訊" : xlSheet.Cells(3, 2) = "饋線狀態" : xlSheet.Cells(3, 5) = 饋線狀態_tb.Text
            xlSheet.Cells(4, 2) = "出  租  方" : xlSheet.Cells(4, 5) = 太_出租_tb.Text : xlSheet.Cells(4, 11) = "聯 絡 人" : xlSheet.Cells(4, 12) = 太_出租_聯_tb.Text
            xlSheet.Cells(5, 2) = "承  租  方" : xlSheet.Cells(5, 5) = 太_承租_tb.Text : xlSheet.Cells(5, 11) = "聯 絡 人" : xlSheet.Cells(5, 12) = 太_承租_聯_tb.Text
            xlSheet.Cells(6, 2) = "KW數" : xlSheet.Cells(6, 5) = KW數_tb.Text : xlSheet.Cells(7, 2) = "租      期" : xlSheet.Cells(7, 5) = 太陽開始_dtp.Text & " - " & 太陽結束_dtp.Text
            xlSheet.Cells(8, 2) = "租金條件(%)" : xlSheet.Cells(8, 5) = 太_租金比_tb.Text : xlSheet.Cells(9, 2) = "並 聯 起 租" : xlSheet.Cells(9, 5) = 並聯起組_tb.Text
            xlSheet.Cells(10, 2) = "地主" : xlSheet.Cells(10, 5) = 地主_tb.Text : xlSheet.Cells(11, 2) = "地址" : xlSheet.Cells(11, 5) = 地址_tb.Text
            AA(xlApp, xlSheet)
            'xlApp.Sheets(2).Delete : xlSheet.PageSetup.PrintArea = ""
            ''----轉成PDF-----
            'Dim SERT As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            'Dim 時間參數 As String = Today.Year.ToString.PadLeft(4, "0") & Today.Month.ToString.PadLeft(2, "0") & Today.Day.ToString.PadLeft(2, "0")
            'xlBook.Sheets(1).ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, SERT & "\太陽能資料表-" & 時間參數 & ".pdf", XlFixedFormatQuality.xlQualityStandard, True)
            ''--------
            'xlApp.Cells.Select() : xlApp.Application.WindowState = xlMinimized : xlBook.Close(False)
            'Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
            'Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
            'xlApp.Quit() : Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
            'xlSheet = Nothing : xlBook = Nothing : xlApp = Nothing : GC.Collect() : GC.WaitForPendingFinalizers()
            'Try
            '    Dim pdfFilePath As String = SERT & "\太陽能資料表-" & 時間參數 & ".pdf"
            '    Dim acrobatReaderPath As String = "C:\Program Files\Google\Chrome\Application\chrome.exe"
            '    If System.IO.File.Exists(acrobatReaderPath) Then : Process.Start(acrobatReaderPath, pdfFilePath)
            '    Else : Dim chromePath As String = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
            '        If System.IO.File.Exists(chromePath) Then : Process.Start(chromePath, pdfFilePath)
            '        Else : Dim edgePath As String = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
            '            If System.IO.File.Exists(edgePath) Then : Process.Start(edgePath, pdfFilePath)
            '            Else : MsgBox("以存檔至桌面 。" & vbCrLf & "檔案名稱：太陽能資料表-" & 時間參數 & ".pdf")
            '            End If : End If : End If
            'Catch ex As Exception
            '    MsgBox("以存檔至桌面 。" & vbCrLf & "檔案名稱：太陽能資料表-" & 時間參數 & ".pdf")
            'End Try

        ElseIf 太陽能列印選項 = "全印" Then
            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(2, 2) = "太陽能資訊"
            For i As Integer = 0 To 太陽_dgv.Rows.Count - 1
                xlSheet.Cells(3 + 9 * i, 2) = "饋線狀態" : xlSheet.Cells(3 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_饋線狀態").Value.ToString
                xlSheet.Cells(4 + 9 * i, 2) = "出  租  方" : xlSheet.Cells(4 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_出租方").Value.ToString
                xlSheet.Cells(4 + 9 * i, 11) = "聯 絡 人" : xlSheet.Cells(4 + 9 * i, 12) = 太陽_dgv.Rows(i).Cells("太陽_出租人").Value.ToString
                xlSheet.Cells(5 + 9 * i, 2) = "承  租  方" : xlSheet.Cells(5 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_承租方").Value.ToString
                xlSheet.Cells(5 + 9 * i, 11) = "聯 絡 人" : xlSheet.Cells(5 + 9 * i, 12) = 太陽_dgv.Rows(i).Cells("太陽_承租人").Value.ToString
                xlSheet.Cells(6 + 9 * i, 2) = "KW數" : xlSheet.Cells(6 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_KW數").Value.ToString
                xlSheet.Cells(7 + 9 * i, 2) = "租      期" : xlSheet.Cells(7 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_租期").Value.ToString
                xlSheet.Cells(8 + 9 * i, 2) = "租金條件(%)" : xlSheet.Cells(8 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_租金%").Value.ToString
                xlSheet.Cells(9 + 9 * i, 2) = "並 聯 起 租" : xlSheet.Cells(9 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_並聯起租").Value.ToString
                If 太陽_dgv.Rows(i).Cells("單據編號").Value.ToString <> "獨" Then
                    xlSheet.Cells(10 + 9 * i, 2) = "單據編號" : xlSheet.Cells(10 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("單據編號").Value.ToString
                    xlSheet.Cells(11 + 9 * i, 2) = "地號" : SQL_取得地號(太陽_dgv.Rows(i).Cells("單據編號").Value.ToString)
                    If dr.Read() Then : xlSheet.Cells(11 + 9 * i, 5) = dr(0).ToString : End If
                Else
                    xlSheet.Cells(10 + 9 * i, 2) = "地主" : xlSheet.Cells(10 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_地主").Value.ToString
                    xlSheet.Cells(11 + 9 * i, 2) = "地址" : xlSheet.Cells(11 + 9 * i, 5) = 太陽_dgv.Rows(i).Cells("太陽_地址").Value.ToString
                End If

            Next
            DD(xlApp, xlSheet)
            xlApp.Sheets(2).Delete : xlSheet.PageSetup.PrintArea = ""
            '----轉成PDF-----
            Dim SERT As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            Dim 時間參數 As String = Today.Year.ToString.PadLeft(4, "0") & Today.Month.ToString.PadLeft(2, "0") & Today.Day.ToString.PadLeft(2, "0")
            xlBook.Sheets(1).ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, SERT & "\太陽能資料表-" & 時間參數 & ".pdf", XlFixedFormatQuality.xlQualityStandard, True)
            '--------
            xlApp.Cells.Select() : xlApp.Application.WindowState = xlMinimized : xlBook.Close(False)
            Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
            Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
            xlApp.Quit() : Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
            xlSheet = Nothing : xlBook = Nothing : xlApp = Nothing : GC.Collect() : GC.WaitForPendingFinalizers()
            Try
                Dim pdfFilePath As String = SERT & "\太陽能資料表-" & 時間參數 & ".pdf"
                Dim acrobatReaderPath As String = "C:\Program Files\Google\Chrome\Application\chrome.exe"
                If System.IO.File.Exists(acrobatReaderPath) Then : Process.Start(acrobatReaderPath, pdfFilePath)
                Else : Dim chromePath As String = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
                    If System.IO.File.Exists(chromePath) Then : Process.Start(chromePath, pdfFilePath)
                    Else : Dim edgePath As String = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
                        If System.IO.File.Exists(edgePath) Then : Process.Start(edgePath, pdfFilePath)
                        Else : MsgBox("以存檔至桌面 。" & vbCrLf & "檔案名稱：太陽能資料表-" & 時間參數 & ".pdf")
                        End If : End If : End If
            Catch ex As Exception
                MsgBox("以存檔至桌面 。" & vbCrLf & "檔案名稱：太陽能資料表-" & 時間參數 & ".pdf")
            End Try
        Else
            MsgBox("沒有選擇列印模式!")
        End If
    End Sub

    Private Sub AA(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Worksheet)
        myExcel.Columns("B:T").Select
        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
        myExcel.Range("B2:T2").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 = 18 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With
        Dim 太陽能開始行數 = 3

        For j = 0 To 8
            myExcel.Range("B" & 太陽能開始行數 + j & ":D" & 太陽能開始行數 + j).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
        Next
        myExcel.Range("E" & 太陽能開始行數 & ":T" & 太陽能開始行數).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
        For j = 1 To 2
            myExcel.Range("E" & 太陽能開始行數 + j & ":J" & 太陽能開始行數 + j).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
            myExcel.Range("L" & 太陽能開始行數 + j & ":T" & 太陽能開始行數 + j).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
            myExcel.Range("K" & 太陽能開始行數 + j & ":K" & 太陽能開始行數 + j).Select()
            With myExcel.Selection
                .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
                .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
            End With
        Next
        For j = 3 To 8
            myExcel.Range("E" & 太陽能開始行數 + j & ":T" & 太陽能開始行數 + j).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
        Next
        myExcel.Columns("B:B").ColumnWidth = 4 : myExcel.Columns("C:C").ColumnWidth = 8 : myExcel.Columns("D:D").ColumnWidth = 8 : myExcel.Columns("E:E").ColumnWidth = 8
        myExcel.Columns("F:F").ColumnWidth = 8 : myExcel.Columns("G:G").ColumnWidth = 14 : myExcel.Columns("H:H").ColumnWidth = 14 : myExcel.Columns("I:I").ColumnWidth = 4
        myExcel.Columns("J:J").ColumnWidth = 14 : myExcel.Columns("K:K").ColumnWidth = 14 : myExcel.Columns("L:L").ColumnWidth = 4 : myExcel.Columns("M:M").ColumnWidth = 8
        myExcel.Columns("N:N").ColumnWidth = 8 : myExcel.Columns("O:O").ColumnWidth = 8 : myExcel.Columns("P:P").ColumnWidth = 8 : myExcel.Columns("Q:Q").ColumnWidth = 14
        myExcel.Columns("R:R").ColumnWidth = 14 : myExcel.Columns("S:S").ColumnWidth = 14 : myExcel.Columns("T:T").ColumnWidth = 15.13

        myExcel.Range("B2:T2").Select()
        With myExcel.Selection.Interior
            .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent4 : .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With

        myExcel.Range("B2:T11").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
        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With

        myExcel.Columns("A:A").ColumnWidth = 0.54
        myExcel.Rows("1:1").RowHeight = 3.75
        '====列印======
        myExcel.Application.PrintCommunication = False : With myExcel.ActiveSheet.PageSetup : .PrintTitleRows = "" : .PrintTitleColumns = "" : .PaperSize = XlPaperSize.xlPaperA4 : End With
        myExcel.Application.PrintCommunication = True : myExcel.ActiveSheet.PageSetup.PrintArea = "" : myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup : .LeftHeader = "" : .CenterHeader = "" : .RightHeader = "" : .LeftFooter = "" : .CenterFooter = "" : .RightFooter = ""
            .LeftMargin = myExcel.Application.InchesToPoints(0.25) : .RightMargin = myExcel.Application.InchesToPoints(0.25) : .TopMargin = myExcel.Application.InchesToPoints(0.75)
            .BottomMargin = myExcel.Application.InchesToPoints(0.75) : .HeaderMargin = myExcel.Application.InchesToPoints(0.3) : .FooterMargin = myExcel.Application.InchesToPoints(0.3)
            .PrintHeadings = False : .PrintGridlines = False : .CenterHorizontally = False : .CenterVertically = False : .Draft = False : .FirstPageNumber = xlAutomatic
            .BlackAndWhite = False : .Zoom = False : .FitToPagesWide = 1 : .FitToPagesTall = False : .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False : .ScaleWithDocHeaderFooter = True : .AlignMarginsHeaderFooter = True : .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = "" : .EvenPage.RightHeader.Text = "" : .EvenPage.LeftFooter.Text = "" : .EvenPage.CenterFooter.Text = "" : .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = "" : .FirstPage.CenterHeader.Text = "" : .FirstPage.RightHeader.Text = "" : .FirstPage.LeftFooter.Text = "" : .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
        myExcel.Application.PrintCommunication = True

    End Sub
    Private Sub DD(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Worksheet)
        myExcel.Columns("B:T").Select
        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
        myExcel.Range("B2:T2").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 = 18 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With
        Dim 太陽能開始行數 = 3
        For i As Integer = 0 To 太陽_dgv.Rows.Count - 1
            For j = 0 To 8
                myExcel.Range("B" & 太陽能開始行數 + j + 9 * i & ":D" & 太陽能開始行數 + j + 9 * 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
                myExcel.Selection.Merge
            Next
            myExcel.Range("E" & 太陽能開始行數 + 9 * i & ":T" & 太陽能開始行數 + 9 * 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
            myExcel.Selection.Merge
            For j = 1 To 2
                myExcel.Range("E" & 太陽能開始行數 + j + 9 * i & ":J" & 太陽能開始行數 + j + 9 * 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
                myExcel.Selection.Merge
                myExcel.Range("L" & 太陽能開始行數 + j + 9 * i & ":T" & 太陽能開始行數 + j + 9 * 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
                myExcel.Selection.Merge
                myExcel.Range("K" & 太陽能開始行數 + j + 9 * i & ":K" & 太陽能開始行數 + j + 9 * 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
            Next
            For j = 3 To 8
                myExcel.Range("E" & 太陽能開始行數 + j + 9 * i & ":T" & 太陽能開始行數 + j + 9 * 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 : myExcel.Selection.Merge
            Next

        Next
        myExcel.Columns("B:B").ColumnWidth = 4 : myExcel.Columns("C:C").ColumnWidth = 8 : myExcel.Columns("D:D").ColumnWidth = 8 : myExcel.Columns("E:E").ColumnWidth = 8
        myExcel.Columns("F:F").ColumnWidth = 8 : myExcel.Columns("G:G").ColumnWidth = 14 : myExcel.Columns("H:H").ColumnWidth = 14 : myExcel.Columns("I:I").ColumnWidth = 4
        myExcel.Columns("J:J").ColumnWidth = 14 : myExcel.Columns("K:K").ColumnWidth = 14 : myExcel.Columns("L:L").ColumnWidth = 4 : myExcel.Columns("M:M").ColumnWidth = 8
        myExcel.Columns("N:N").ColumnWidth = 8 : myExcel.Columns("O:O").ColumnWidth = 8 : myExcel.Columns("P:P").ColumnWidth = 8 : myExcel.Columns("Q:Q").ColumnWidth = 14
        myExcel.Columns("R:R").ColumnWidth = 14 : myExcel.Columns("S:S").ColumnWidth = 14 : myExcel.Columns("T:T").ColumnWidth = 15.13




        myExcel.Range("B2:T2").Select()
        With myExcel.Selection.Interior
            .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent4 : .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With

        myExcel.Range("B2:T" & 太陽能開始行數 + 9 * 太陽_dgv.Rows.Count - 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
        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With

        myExcel.Columns("A:A").ColumnWidth = 0.54
        myExcel.Rows("1:1").RowHeight = 3.75
        '====列印======
        myExcel.Application.PrintCommunication = False : With myExcel.ActiveSheet.PageSetup : .PrintTitleRows = "" : .PrintTitleColumns = "" : .PaperSize = XlPaperSize.xlPaperA4 : End With
        myExcel.Application.PrintCommunication = True : myExcel.ActiveSheet.PageSetup.PrintArea = "" : myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup : .LeftHeader = "" : .CenterHeader = "" : .RightHeader = "" : .LeftFooter = "" : .CenterFooter = "" : .RightFooter = ""
            .LeftMargin = myExcel.Application.InchesToPoints(0.25) : .RightMargin = myExcel.Application.InchesToPoints(0.25) : .TopMargin = myExcel.Application.InchesToPoints(0.75)
            .BottomMargin = myExcel.Application.InchesToPoints(0.75) : .HeaderMargin = myExcel.Application.InchesToPoints(0.3) : .FooterMargin = myExcel.Application.InchesToPoints(0.3)
            .PrintHeadings = False : .PrintGridlines = False : .CenterHorizontally = False : .CenterVertically = False : .Draft = False : .FirstPageNumber = xlAutomatic
            .BlackAndWhite = False : .Zoom = False : .FitToPagesWide = 1 : .FitToPagesTall = False : .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False : .ScaleWithDocHeaderFooter = True : .AlignMarginsHeaderFooter = True : .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = "" : .EvenPage.RightHeader.Text = "" : .EvenPage.LeftFooter.Text = "" : .EvenPage.CenterFooter.Text = "" : .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = "" : .FirstPage.CenterHeader.Text = "" : .FirstPage.RightHeader.Text = "" : .FirstPage.LeftFooter.Text = "" : .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
        myExcel.Application.PrintCommunication = True
    End Sub
    Private Sub 太陽開始_dtp_MouseDown(sender As Object, e As EventArgs) Handles 太陽開始_dtp.MouseDown
        太陽開始_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 太陽結束_dtp_MouseDown(sender As Object, e As EventArgs) Handles 太陽結束_dtp.MouseDown
        太陽結束_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 同出租方_bt_Click(sender As Object, e As EventArgs) Handles 同出租方_bt.Click
        地主_tb.Text = 太_出租_tb.Text
    End Sub

    Private Sub 清除5_bt_Click(sender As Object, e As EventArgs) Handles 清除5_bt.Click
        太陽開始_dtp.Format = DateTimePickerFormat.Custom : 太陽開始_dtp.CustomFormat = " " : 太陽結束_dtp.Format = DateTimePickerFormat.Custom : 太陽結束_dtp.CustomFormat = " "
    End Sub

    Private Sub YS員工_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles YS員工_cb.SelectedIndexChanged
        If 已開啟 Then
            饋線狀態_tb.Text = "" : 太_出租_tb.Text = "" : 太_出租_聯_tb.Text = "" : 太_承租_tb.Text = "" : 太_承租_聯_tb.Text = "" : KW數_tb.Text = "" : 太_租金比_tb.Text = ""
            並聯起組_tb.Text = "" : 地主_tb.Text = "" : 地址_tb.Text = "" : 太陽流水號_tb.Text = "" : 太陽開始_dtp.Format = DateTimePickerFormat.Custom
            太陽開始_dtp.CustomFormat = " " : 太陽結束_dtp.Format = DateTimePickerFormat.Custom : 太陽結束_dtp.CustomFormat = " " : Set_清單()
        End If
    End Sub
End Class