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
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core

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
        清單1_dgv.DataSource = Nothing : ds1.Clear()
        清單1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        清單1_dgv.ColumnHeadersHeight = 45 : 清單1_dgv.AllowUserToAddRows = False : 清單1_dgv.RowTemplate.Height = 45
        清單1_dgv.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
        If 全部資料_ch.Checked = True Then
            SQL2 = " OR (案件摘要_承租.承租狀態 LIKE '%%') OR (案件摘要_承租.承租狀態 IS NULL) "
        Else : SQL2 = ""
            If 未租用_ch.Checked = True Then : SQL2 &= " OR (案件摘要_承租.承租狀態 IS NULL) " : End If
            If S_ch.Checked = True Then : SQL2 &= " OR (案件摘要_承租.承租狀態 LIKE 'S') " : End If
            If H_ch.Checked = True Then : SQL2 &= " OR (案件摘要_承租.承租狀態 LIKE 'H') " : End If
            If A_ch.Checked = True Then : SQL2 &= " OR (案件摘要_承租.承租狀態 LIKE 'A') " : End If
            If B_ch.Checked = True Then : SQL2 &= " OR (案件摘要_承租.承租狀態 LIKE 'B') " : End If
            If F_ch.Checked = True Then : SQL2 &= " OR (案件摘要_承租.承租狀態 LIKE 'F') " : End If
        End If
        If SQL2 = "" Then : 全部資料_ch.Checked = True : SQL2 = " OR (案件摘要_承租.承租狀態 LIKE '%%') OR (案件摘要_承租.承租狀態 IS NULL) " : End If
        If SQL2.StartsWith(" OR") Then : SQL2 = SQL2.Substring(3) : End If
        SQL_案場清單讀取()
        da.Fill(ds1) : 清單1_dgv.DataSource = ds1.Tables(0) : conn.Close()

        清單1_dgv.Columns(0).Frozen = True : 清單1_dgv.Columns(1).Frozen = True : 清單1_dgv.Columns(2).Frozen = True

        清單1_dgv.Columns("地主").Width = 100 : 清單1_dgv.Columns("建坪編號").Width = 40 : 清單1_dgv.Columns("地址/地號").Width = 120 : 清單1_dgv.Columns("坪數/區塊面積").Width = 140
        清單1_dgv.Columns("備註").Width = 100 : 清單1_dgv.Columns("坪數").Width = 100 : 清單1_dgv.Columns("使用用途").Width = 80 : 清單1_dgv.Columns("土地租期").Width = 160
        清單1_dgv.Columns("承租狀態").Width = 150 : 清單1_dgv.Columns("客戶名稱").Width = 100 : 清單1_dgv.Columns("類別").Width = 50 : 清單1_dgv.Columns("客免租期").Width = 160
        清單1_dgv.Columns("客戶起租").Width = 160
        清單1_dgv.Columns("客戶起租").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns("客免租期").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns("類別").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns("承租狀態").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns("土地租期").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns("使用用途").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns("坪數").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns("建坪編號").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        If 備註_ch.Checked Then : 清單1_dgv.Columns("備註").Visible = True : Else : 清單1_dgv.Columns("備註").Visible = False : End If
        If 坪數_ch.Checked Then : 清單1_dgv.Columns("坪數").Visible = True : Else : 清單1_dgv.Columns("坪數").Visible = False : End If
        If 使用用途_ch.Checked Then : 清單1_dgv.Columns("使用用途").Visible = True : Else : 清單1_dgv.Columns("使用用途").Visible = False : End If
        If 土地租期_ch.Checked Then : 清單1_dgv.Columns("土地租期").Visible = True : Else : 清單1_dgv.Columns("土地租期").Visible = False : End If
        If 承租狀態_ch.Checked Then : 清單1_dgv.Columns("承租狀態").Visible = True : Else : 清單1_dgv.Columns("承租狀態").Visible = False : End If
        If 客戶名稱_ch.Checked Then : 清單1_dgv.Columns("客戶名稱").Visible = True : Else : 清單1_dgv.Columns("客戶名稱").Visible = False : End If
        If 類別_ch.Checked Then : 清單1_dgv.Columns("類別").Visible = True : Else : 清單1_dgv.Columns("類別").Visible = False : End If
        If 免租期_ch.Checked Then : 清單1_dgv.Columns("客免租期").Visible = True : Else : 清單1_dgv.Columns("客免租期").Visible = False : End If
        If 租期_ch.Checked Then : 清單1_dgv.Columns("客戶起租").Visible = True : Else : 清單1_dgv.Columns("客戶起租").Visible = False : End If
        If 租期結束_ch.Checked Then : 清單1_dgv.Columns("客租到期").Visible = True : Else : 清單1_dgv.Columns("客租到期").Visible = False : End If
        If 坪數區塊面積_ch.Checked Then : 清單1_dgv.Columns("坪數/區塊面積").Visible = True : Else : 清單1_dgv.Columns("坪數/區塊面積").Visible = False : End If
        If 面寬_ch.Checked Then : 清單1_dgv.Columns("面寬").Visible = True : Else : 清單1_dgv.Columns("面寬").Visible = False : End If
        If 深度_ch.Checked Then : 清單1_dgv.Columns("深度").Visible = True : Else : 清單1_dgv.Columns("深度").Visible = False : End If
        If 高度_ch.Checked Then : 清單1_dgv.Columns("高度").Visible = True : Else : 清單1_dgv.Columns("高度").Visible = False : End If
        If 滴水高度_ch.Checked Then : 清單1_dgv.Columns("滴水高度").Visible = True : Else : 清單1_dgv.Columns("滴水高度").Visible = False : End If
        If 屋面方向_ch.Checked Then : 清單1_dgv.Columns("屋面方向").Visible = True : Else : 清單1_dgv.Columns("屋面方向").Visible = False : End If
        If 建照號碼_ch.Checked Then : 清單1_dgv.Columns("建照號碼").Visible = True : Else : 清單1_dgv.Columns("建照號碼").Visible = False : End If
        If 使用執照號碼_ch.Checked Then : 清單1_dgv.Columns("使用執照號碼").Visible = True : Else : 清單1_dgv.Columns("使用執照號碼").Visible = False : End If
        If 建號_ch.Checked Then : 清單1_dgv.Columns("建號").Visible = True : Else : 清單1_dgv.Columns("建號").Visible = False : End If
        If 其他約定事項_ch.Checked Then : 清單1_dgv.Columns("其他約定事項").Visible = True : Else : 清單1_dgv.Columns("其他約定事項").Visible = False : End If
        If 面積土地_ch.Checked Then : 清單1_dgv.Columns("面積土地").Visible = True : Else : 清單1_dgv.Columns("面積土地").Visible = False : End If
        If 面積建物_ch.Checked Then : 清單1_dgv.Columns("面積建物").Visible = True : Else : 清單1_dgv.Columns("面積建物").Visible = False : End If
        If 容積率_ch.Checked Then : 清單1_dgv.Columns("容積率").Visible = True : Else : 清單1_dgv.Columns("容積率").Visible = False : End If
        If 地租到期_ch.Checked Then : 清單1_dgv.Columns("地租到期").Visible = True : Else : 清單1_dgv.Columns("地租到期").Visible = False : End If
        For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
            If 租期_ch.Checked Then
                清單1_dgv.Rows(i).Cells("客戶起租").Value = 清單1_dgv.Rows(i).Cells("客戶起租").Value.ToString & " - " & 清單1_dgv.Rows(i).Cells("客租到期").Value.ToString
            End If
            If 坪數_ch.Checked Then
                清單1_dgv.Rows(i).Cells("坪數").Value = Strings.Format(CDbl(清單1_dgv.Rows(i).Cells("坪數").Value.ToString), "#,##0.0")
            End If
            If 承租狀態_ch.Checked Then
                If 清單1_dgv.Rows(i).Cells("承租狀態").Value.ToString = "S" Then
                    清單1_dgv.Rows(i).Cells("承租狀態").Value = "S:已簽正式約"
                ElseIf 清單1_dgv.Rows(i).Cells("承租狀態").Value.ToString = "H" Then
                    清單1_dgv.Rows(i).Cells("承租狀態").Value = "H:已簽意向書及付訂金"
                ElseIf 清單1_dgv.Rows(i).Cells("承租狀態").Value.ToString = "A" Then
                    清單1_dgv.Rows(i).Cells("承租狀態").Value = "A:房客開發人員評估確度高,一個月內可簽約"
                ElseIf 清單1_dgv.Rows(i).Cells("承租狀態").Value.ToString = "B" Then
                    清單1_dgv.Rows(i).Cells("承租狀態").Value = "B:房客開發評估中"
                ElseIf 清單1_dgv.Rows(i).Cells("承租狀態").Value.ToString = "F" Then
                    清單1_dgv.Rows(i).Cells("承租狀態").Value = "F:房客無意願"
                End If
            End If
        Next
    End Sub
    Private Sub 案場清單_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        已開啟 = False : Me.MdiParent = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
    End Sub
    Private Sub 案場清單_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        SQL_年份() : 年份_cb.Items.Clear() : 年份_cb.Items.Add("") : While (dr.Read()) : 年份_cb.Items.Add(dr("年份")) : End While : conn.Close() : 年份_cb.Text = Today.Year.ToString()
        備註_ch.Checked = True : 坪數_ch.Checked = True : 使用用途_ch.Checked = True : 土地租期_ch.Checked = True : 地租到期_ch.Checked = True : 免租期_ch.Checked = True
        類別_ch.Checked = True : 承租狀態_ch.Checked = True : 客戶名稱_ch.Checked = True : 租期_ch.Checked = True : 租期結束_ch.Checked = False
        坪數區塊面積_ch.Checked = True
        面寬_ch.Checked = False : 深度_ch.Checked = False : 高度_ch.Checked = False : 滴水高度_ch.Checked = False : 屋面方向_ch.Checked = False
        建照號碼_ch.Checked = False : 使用執照號碼_ch.Checked = False : 建號_ch.Checked = False : 其他約定事項_ch.Checked = False : 面積土地_ch.Checked = False
        面積建物_ch.Checked = False : 容積率_ch.Checked = False
        承租狀態_pl.Visible = False : 表頭清單_pl.Visible = False
        Set_案場清單() : 已開啟 = True
    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 清單1_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 清單1_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 清單1_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 清單1_dgv("地址/地號", e.RowIndex).Value.ToString <> 清單1_dgv("地址/地號", e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(清單1_dgv.RowHeadersVisible, 清單1_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 清單1_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 清單1_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub 重讀_bt_Click(sender As Object, e As EventArgs) Handles 重讀_bt.Click
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub 全部資料_ch_Click(sender As Object, e As EventArgs) Handles 全部資料_ch.Click
        全部資料_ch.Checked = True : 未租用_ch.Checked = False : S_ch.Checked = False : H_ch.Checked = False : A_ch.Checked = False
        B_ch.Checked = False : F_ch.Checked = False
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub 未租用_ch_Click(sender As Object, e As EventArgs) Handles 未租用_ch.Click
        全部資料_ch.Checked = False : 未租用_ch.Checked = True : S_ch.Checked = False : H_ch.Checked = False : A_ch.Checked = False
        B_ch.Checked = False : F_ch.Checked = False
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub S1_ch_Click(sender As Object, e As EventArgs) Handles S_ch.Click
        全部資料_ch.Checked = False : 未租用_ch.Checked = False : S_ch.Checked = True : H_ch.Checked = False : A_ch.Checked = False
        B_ch.Checked = False : F_ch.Checked = False
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub H_ch_Click(sender As Object, e As EventArgs) Handles H_ch.Click
        全部資料_ch.Checked = False : 未租用_ch.Checked = False : S_ch.Checked = False : H_ch.Checked = True : A_ch.Checked = False
        B_ch.Checked = False : F_ch.Checked = False
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub A_ch_Click(sender As Object, e As EventArgs) Handles A_ch.Click
        全部資料_ch.Checked = False : 未租用_ch.Checked = False : S_ch.Checked = False : H_ch.Checked = False : A_ch.Checked = True
        B_ch.Checked = False : F_ch.Checked = False
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub B_ch_Click(sender As Object, e As EventArgs) Handles B_ch.Click
        全部資料_ch.Checked = False : 未租用_ch.Checked = False : S_ch.Checked = False : H_ch.Checked = False : A_ch.Checked = False
        B_ch.Checked = True : F_ch.Checked = False
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub F_ch_Click(sender As Object, e As EventArgs) Handles F_ch.Click
        全部資料_ch.Checked = False : 未租用_ch.Checked = False : S_ch.Checked = False : H_ch.Checked = False : A_ch.Checked = False
        B_ch.Checked = False : F_ch.Checked = True
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub 選擇承租狀態_bt_Click(sender As Object, e As EventArgs) Handles 選擇承租狀態_bt.Click
        If 承租狀態_pl.Visible = False Then : 承租狀態_pl.Visible = True : Else : 承租狀態_pl.Visible = False : End If
    End Sub
    Private Sub 收合_bt_Click(sender As Object, e As EventArgs) Handles 收合_bt.Click
        承租狀態_pl.Visible = False
    End Sub
    Private Sub 收合2_bt_Click(sender As Object, e As EventArgs) Handles 收合2_bt.Click
        表頭清單_pl.Visible = False
    End Sub
    Private Sub 清單表頭設定_bt_Click(sender As Object, e As EventArgs) Handles 清單表頭設定_bt.Click
        If 表頭清單_pl.Visible = False Then : 表頭清單_pl.Visible = True : Else : 表頭清單_pl.Visible = False : End If
    End Sub
    Private Sub 系統預設_bt_Click(sender As Object, e As EventArgs) Handles 系統預設_bt.Click
        備註_ch.Checked = True : 坪數_ch.Checked = True : 使用用途_ch.Checked = True : 土地租期_ch.Checked = True : 地租到期_ch.Checked = True : 免租期_ch.Checked = True
        類別_ch.Checked = True : 承租狀態_ch.Checked = True : 客戶名稱_ch.Checked = True : 租期_ch.Checked = True : 租期結束_ch.Checked = False
        坪數區塊面積_ch.Checked = True
        面寬_ch.Checked = False : 深度_ch.Checked = False : 高度_ch.Checked = False : 滴水高度_ch.Checked = False : 屋面方向_ch.Checked = False
        建照號碼_ch.Checked = False : 使用執照號碼_ch.Checked = False : 建號_ch.Checked = False : 其他約定事項_ch.Checked = False : 面積土地_ch.Checked = False
        面積建物_ch.Checked = False : 容積率_ch.Checked = False
        If 已開啟 Then : Set_案場清單() : End If
    End Sub
    Private Sub 重讀1_bt_Click(sender As Object, e As EventArgs) Handles 重讀1_bt.Click
        重讀_bt.PerformClick() : 收合2_bt.PerformClick()
    End Sub
    Private Sub 全選_bt_Click(sender As Object, e As EventArgs) Handles 全選_bt.Click
        備註_ch.Checked = True : 坪數_ch.Checked = True : 使用用途_ch.Checked = True : 土地租期_ch.Checked = True : 地租到期_ch.Checked = True : 免租期_ch.Checked = True
        類別_ch.Checked = True : 承租狀態_ch.Checked = True : 客戶名稱_ch.Checked = True : 租期_ch.Checked = True : 租期結束_ch.Checked = True
        坪數區塊面積_ch.Checked = True
        面寬_ch.Checked = True : 深度_ch.Checked = True : 高度_ch.Checked = True : 滴水高度_ch.Checked = True : 屋面方向_ch.Checked = True
        建照號碼_ch.Checked = True : 使用執照號碼_ch.Checked = True : 建號_ch.Checked = True : 其他約定事項_ch.Checked = True : 面積土地_ch.Checked = True
        面積建物_ch.Checked = True : 容積率_ch.Checked = True
        If 已開啟 Then : Set_案場清單() : 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
        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) = "案場清單"
        Dim 列數 As Integer = 2
        For i As Integer = 0 To 清單1_dgv.Columns.Count - 1
            If 清單1_dgv.Columns(i).Name = "地址/地號" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 清單1_dgv.Columns(i).Name = "地主" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 清單1_dgv.Columns(i).Name = "建坪編號" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 備註_ch.Checked And 清單1_dgv.Columns(i).Name = "備註" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 坪數_ch.Checked And 清單1_dgv.Columns(i).Name = "坪數" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 使用用途_ch.Checked And 清單1_dgv.Columns(i).Name = "使用用途" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 土地租期_ch.Checked And 清單1_dgv.Columns(i).Name = "土地租期" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 地租到期_ch.Checked And 清單1_dgv.Columns(i).Name = "地租到期" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 承租狀態_ch.Checked And 清單1_dgv.Columns(i).Name = "承租狀態" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 客戶名稱_ch.Checked And 清單1_dgv.Columns(i).Name = "客戶名稱" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 類別_ch.Checked And 清單1_dgv.Columns(i).Name = "類別" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 免租期_ch.Checked And 清單1_dgv.Columns(i).Name = "客免租期" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 租期_ch.Checked And 清單1_dgv.Columns(i).Name = "客戶起租" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 租期結束_ch.Checked And 清單1_dgv.Columns(i).Name = "客租到期" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 坪數區塊面積_ch.Checked And 清單1_dgv.Columns(i).Name = "坪數/區塊面積" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 面寬_ch.Checked And 清單1_dgv.Columns(i).Name = "面寬" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 深度_ch.Checked And 清單1_dgv.Columns(i).Name = "深度" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 高度_ch.Checked And 清單1_dgv.Columns(i).Name = "高度" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 滴水高度_ch.Checked And 清單1_dgv.Columns(i).Name = "滴水高度" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 屋面方向_ch.Checked And 清單1_dgv.Columns(i).Name = "屋面方向" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 建照號碼_ch.Checked And 清單1_dgv.Columns(i).Name = "建照號碼" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 使用執照號碼_ch.Checked And 清單1_dgv.Columns(i).Name = "使用執照號碼" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 建號_ch.Checked And 清單1_dgv.Columns(i).Name = "建號" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 其他約定事項_ch.Checked And 清單1_dgv.Columns(i).Name = "其他約定事項" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 面積土地_ch.Checked And 清單1_dgv.Columns(i).Name = "面積土地" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 面積建物_ch.Checked And 清單1_dgv.Columns(i).Name = "面積建物" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
            If 容積率_ch.Checked And 清單1_dgv.Columns(i).Name = "容積率" Then : xlSheet.Cells(4, 列數) = 清單1_dgv.Columns(i).Name : 列數 += 1 : End If
        Next
        For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
            列數 = 2
            xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("地址/地號").Value.ToString() : 列數 += 1
            xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("地主").Value.ToString() : 列數 += 1
            xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("建坪編號").Value.ToString() : 列數 += 1
            If 備註_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("備註").Value.ToString() : 列數 += 1 : End If
            If 坪數_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("坪數").Value.ToString() : 列數 += 1 : End If
            If 使用用途_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("使用用途").Value.ToString() : 列數 += 1 : End If
            If 土地租期_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("土地租期").Value.ToString() : 列數 += 1 : End If
            If 地租到期_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("地租到期").Value.ToString() : 列數 += 1 : End If
            If 承租狀態_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("承租狀態").Value.ToString() : 列數 += 1 : End If
            If 客戶名稱_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("客戶名稱").Value.ToString() : 列數 += 1 : End If
            If 類別_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("類別").Value.ToString() : 列數 += 1 : End If
            If 免租期_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("客免租期").Value.ToString() : 列數 += 1 : End If
            If 租期_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("客戶起租").Value.ToString() : 列數 += 1 : End If
            If 租期結束_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("客租到期").Value.ToString() : 列數 += 1 : End If
            If 坪數區塊面積_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("坪數/區塊面積").Value.ToString() : 列數 += 1 : End If
            If 面寬_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("面寬").Value.ToString() : 列數 += 1 : End If
            If 深度_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("深度").Value.ToString() : 列數 += 1 : End If
            If 高度_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("高度").Value.ToString() : 列數 += 1 : End If
            If 滴水高度_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("滴水高度").Value.ToString() : 列數 += 1 : End If
            If 屋面方向_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("屋面方向").Value.ToString() : 列數 += 1 : End If
            If 建照號碼_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("建照號碼").Value.ToString() : 列數 += 1 : End If
            If 使用執照號碼_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("使用執照號碼").Value.ToString() : 列數 += 1 : End If
            If 建號_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("建號").Value.ToString() : 列數 += 1 : End If
            If 其他約定事項_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("其他約定事項").Value.ToString() : 列數 += 1 : End If
            If 面積土地_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("面積土地").Value.ToString() : 列數 += 1 : End If
            If 面積建物_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("面積建物").Value.ToString() : 列數 += 1 : End If
            If 容積率_ch.Checked Then : xlSheet.Cells(5 + i, 列數) = 清單1_dgv.Rows(i).Cells("容積率").Value.ToString() : 列數 += 1 : End If
        Next
        AA(xlApp, xlSheet, 列數 - 1)
        DD(xlApp, xlSheet)
    End Sub
    Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet, 列數 As Integer)
        Dim 英文 As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB"}
        myExcel.Columns("B:" & 英文(列數 - 1)).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
        myExcel.Range("B2:" & 英文(列數 - 1) & "3").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 = 24 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With
        myExcel.Range("B4:" & 英文(列數 - 1) & "4").Select()
        With myExcel.Selection
            .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With
        With myExcel.Selection.Interior
            .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent4 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0
        End With


        For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
            Dim 列數2 As Integer = 2
            myExcel.Range(英文(列數2 - 1) & 5 + i).Select()
            With myExcel.Selection
                .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
                .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
            End With
            myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 30 : 列數2 += 1
            myExcel.Range(英文(列數2 - 1) & 5 + i).Select()
            With myExcel.Selection
                .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
                .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
            End With
            myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 15 : 列數2 += 1
            myExcel.Range(英文(列數2 - 1) & 5 + 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 : 列數2 += 1
            If 備註_ch.Checked Then : myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 15 : 列數2 += 1 : End If
            If 坪數_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & 5 + 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 : 列數2 += 1
            End If
            If 使用用途_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & 5 + 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.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 15 : 列數2 += 1
            End If
            If 土地租期_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & 5 + 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.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 25 : 列數2 += 1
            End If
            If 地租到期_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & 5 + 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.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 15 : 列數2 += 1
            End If
            If 承租狀態_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & 5 + i).Select()
                With myExcel.Selection
                    .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
                    .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
                End With
                myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 35 : 列數2 += 1
            End If
            If 客戶名稱_ch.Checked Then : myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 26 : 列數2 += 1 : End If
            If 類別_ch.Checked Then : myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 13 : 列數2 += 1 : End If
            If 免租期_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & +5 + 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.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 35 : 列數2 += 1
            End If
            If 租期_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & +5 + 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.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 35 : 列數2 += 1
            End If
            If 租期結束_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & +5 + i).Select()
                With myExcel.Selection
                    .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False
                    .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
                End With
                myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 22 : 列數2 += 1
            End If
            If 坪數區塊面積_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & +5 + i).Select()
                With myExcel.Selection
                    .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False
                    .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
                End With
                myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 30 : 列數2 += 1
            End If
            If 面寬_ch.Checked Then : 列數2 += 1 : End If
            If 深度_ch.Checked Then : 列數2 += 1 : End If
            If 高度_ch.Checked Then : 列數2 += 1 : End If
            If 滴水高度_ch.Checked Then : 列數2 += 1 : End If
            If 屋面方向_ch.Checked Then : 列數2 += 1 : End If
            If 建照號碼_ch.Checked Then : myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 15 : 列數2 += 1 : End If
            If 使用執照號碼_ch.Checked Then : myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 15 : 列數2 += 1 : End If
            If 建號_ch.Checked Then : myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 15 : 列數2 += 1 : End If
            If 其他約定事項_ch.Checked Then
                myExcel.Range(英文(列數2 - 1) & +5 + i).Select()
                With myExcel.Selection
                    .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False
                    .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
                End With
                myExcel.Columns(英文(列數2 - 1) & ":" & 英文(列數2 - 1)).ColumnWidth = 30 : 列數2 += 1
            End If
            If 面積土地_ch.Checked Then : 列數2 += 1 : End If
            If 面積建物_ch.Checked Then : 列數2 += 1 : End If
            If 容積率_ch.Checked Then : 列數2 += 1 : End If
        Next
        myExcel.Rows("2:4").Select : myExcel.Selection.RowHeight = 30
        myExcel.Rows("5:" & 清單1_dgv.Rows.Count + 4).Select : myExcel.Selection.RowHeight = 40
        myExcel.Range("B4:" & 英文(列數 - 1) & 清單1_dgv.Rows.Count + 4).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.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)
        Dim imagePath As String = System.Windows.Forms.Application.StartupPath & "\YSLOGO2.png"
        Dim cell As Range = xlSheet.Cells(2, 2) : Dim left As Single = CType(cell.Left, Single) : Dim top As Single = CType(cell.Top, Single)
        Dim width As Single = 100 : Dim height As Single = 32 + 16
        If Not System.IO.File.Exists(imagePath) Then : MessageBox.Show("圖片文件未找到: " & imagePath) : Return : End If
        xlSheet.Shapes.AddPicture(imagePath, MsoTriState.msoFalse, MsoTriState.msoCTrue, left, top, width, height)
    End Sub
    Private Sub 清單1_dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles 清單1_dgv.CellClick
        If 已開啟 Then
            If 承租狀態_pl.Visible Then : 承租狀態_pl.Visible = False : End If : If 表頭清單_pl.Visible Then : 表頭清單_pl.Visible = False : End If
        End If
    End Sub
End Class