Public Class Frm_Export
    Dim fn As String = ""

    Dim 단위면적당신재생1차에너지합 As Decimal = 0
    Dim 단위면적당1차에너소비량합 As Decimal = 0
    Dim 에너지자립률 As Double = 0

    Public Sub New()

        ' 이 호출은 디자이너에 필요합니다.
        InitializeComponent()
        fn = ""
        ' InitializeComponent() 호출 뒤에 초기화 코드를 추가하십시오.

    End Sub
    Public Sub New(_file As String)

        ' 이 호출은 디자이너에 필요합니다.
        InitializeComponent()
        fn = _file
        ' InitializeComponent() 호출 뒤에 초기화 코드를 추가하십시오.

    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '    Dim prjfile As String = Prj.FileName.Substring(Prj.FileName.LastIndexOf("\") + 1)
        Dim path As String = AppDomain.CurrentDomain.BaseDirectory + "\Export\" + Now.ToString("yyMMdd")
        MakeData(path)
    End Sub


    Private Sub MakeData(savepath As String, Optional silent As Boolean = False)

        'FpSpread1.Visible = False
        ' FpSpread1.Sheets.Clear()

        Dim prjfile As String = Prj.FileName.Substring(Prj.FileName.LastIndexOf("\") + 1)
        prjfile = prjfile.Substring(0, prjfile.LastIndexOf("."))
        Dim di As New System.IO.DirectoryInfo(savepath + "\" + prjfile)
        If Not di.Exists Then di.Create()

        '//테이블갯수만큼 시트를 생성한다.
        For i As Integer = 0 To DSET1.Tables.Count - 1

            Dim dt As DataTable = DSET1.Tables(i)
            Dim tablename As String = dt.TableName.ToLower()
            If tablename = "tbl_pic" Then Continue For
            If tablename.IndexOf("weather") <> -1 Then Continue For
            If tablename = "tbl_buha" Then Continue For
            If tablename = "tbl_monuse" Then Continue For
            If tablename = "tbl_common" Then Continue For
            If tablename = "tbl_profile" Then Continue For


            Dim filename As String = tablename
            If tablename = "tbl_bunbae" Then filename = "냉방분배"
            If tablename = "tbl_common" Then filename = "공용코드"
            If tablename = "tbl_desc" Then filename = "건물개요"
            If tablename = "tbl_kongjo" Then filename = "공조기기"
            If tablename = "tbl_kongkub" Then filename = "공급기기"
            If tablename = "tbl_myoun" Then filename = "입력면"
            If tablename = "tbl_nanbangkiki" Then filename = "난방기기"
            If tablename = "tbl_nangbangkiki" Then filename = "냉방기기"
            If tablename = "tbl_nbunbae" Then filename = "난방분배"
            If tablename = "tbl_new" Then filename = "신재생기기"
            If tablename = "tbl_profile" Then filename = "용도프로필"
            If tablename = "tbl_yk" Then filename = "열관류율"
            If tablename = "tbl_ykdetail" Then filename = "열관류율(세부)"
            If tablename = "tbl_zone" Then filename = "입력존"


            Dim fi As New System.IO.FileInfo(di.FullName + "\" + filename + ".csv")
            Dim buffer As New System.Text.StringBuilder

            'Dim newsheep As New FarPoint.Win.Spread.SheetView(dt.TableName)
            'FpSpread1.Sheets.Add(newsheep)
            'FpSpread1.Sheets(FpSpread1.Sheets.Count - 1).RowCount = dt.Rows.Count + 1
            'FpSpread1.Sheets(FpSpread1.Sheets.Count - 1).ColumnCount = dt.Columns.Count

            '//첫줄에는 컬럼명을 넣는다.
            Dim cols As New List(Of String)
            For c As Integer = 0 To dt.Columns.Count - 1
                'FpSpread1.Sheets(FpSpread1.Sheets.Count - 1).Cells(0, c).Text = dt.Columns(c).ColumnName
                Dim colname = dt.Columns(c).ColumnName
                If colname = "sortkey" Then Continue For

                If tablename = "tbl_ykdetail" Then
                    If colname = "전경색" Or colname = "후경색" Or colname = "커스텀" Then
                        Continue For
                    End If
                ElseIf tablename = "tbl_nbunbae" Then
                    If colname = "지관장소" Then Continue For
                ElseIf tablename = "tbl_myoun" Then
                    If colname = "대차대조존" Then Continue For
                ElseIf tablename = "tbl_zone" Then
                    If colname = "지역" Then Continue For
                    If colname = "냉난방분배시스템" Then Continue For
                End If
                cols.Add(colname)
            Next

            '//column title
            For Each colname As String In cols
                buffer.Append("," + colname)
            Next
            buffer.AppendLine()

            '//이테이블의 데이터를 넣는다.
            For r As Integer = 0 To dt.Rows.Count - 1

                Try
                    Dim codedata As String = dt.Rows(r)("code").ToString()
                    If codedata = "0" Then Continue For
                Catch ex As Exception

                End Try


                For Each colname As String In cols
                    'FpSpread1.Sheets(FpSpread1.Sheets.Count - 1).Cells(r + 1, c).Text = dt.Rows(r)(c).ToString()


                    Dim data As String = dt.Rows(r)(colname).ToString().Replace(",", "_")
                    If tablename = "tbl_nanbangkiki" Then
                        If colname = "연결된시스템" Then
                            Dim dr() As DataRow = DSET1.tbl_new.Select("code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("설명").ToString()
                            End If
                        End If
                    ElseIf tablename = "tbl_myoun" Then
                        '존분류		열관류율2
                        If colname = "존분류" Then
                            Dim dr() As DataRow = DSET1.tbl_zone.Select("code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("설명").ToString()
                            End If
                        ElseIf colname = "열관류율2" Then
                            Dim dr() As DataRow = DSET1.tbl_yk.Select("code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("설명").ToString()
                            End If
                        ElseIf colname = "열관류율" Then
                            Dim yk2code As String = dt.Rows(r)("열관류율2").ToString()
                            If yk2code <> "" AndAlso yk2code <> "0" Then
                                Dim dr() As DataRow = DSET1.tbl_yk.Select("code='" + yk2code + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("열관류율").ToString()
                                End If
                            End If
                        End If
                    ElseIf tablename = "tbl_yk" Then
                        If colname = "면형태" Then
                            Dim dr() As DataRow = DSET1.tbl_common.Select("gubun ='1088' and code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("name").ToString()
                            End If
                        End If
                    ElseIf tablename = "tbl_nbunbae" Then
                        If colname = "생산기기구분" Then
                            Dim dr() As DataRow = DSET1.tbl_nanbangkiki.Select("code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("설명").ToString()
                            End If
                        End If

                    ElseIf tablename = "tbl_kongkub" Then
                        If colname = "열공급생산기기" Then
                            Dim dr() As DataRow = DSET1.tbl_nanbangkiki.Select("code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("설명").ToString()
                            End If
                        End If
                    ElseIf tablename = "tbl_nangbangkiki" Then
                        If colname = "연결된시스템" Then
                            Dim dr() As DataRow = DSET1.tbl_new.Select("code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("설명").ToString()
                            End If
                        ElseIf colname = "온수열생산기기" Then
                            Dim dr() As DataRow = DSET1.tbl_nanbangkiki.Select("code='" + data + "'")
                            If dr.Length = 1 Then
                                data = dr(0)("설명").ToString()
                            End If
                        End If
                    ElseIf tablename = "tbl_zone" Then

                        Select Case colname
                            Case "프로필"
                                Dim dr() As DataRow = DSET1.tbl_profile.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "냉난방열공급시스템"
                                Dim dr() As DataRow = DSET1.tbl_kongkub.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "냉난방공조처리시스템"
                                Dim dr() As DataRow = DSET1.tbl_kongjo.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "열생산난방생산기기"
                                Dim dr() As DataRow = DSET1.tbl_nanbangkiki.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "공조냉방생산기기"
                                Dim dr() As DataRow = DSET1.tbl_nangbangkiki.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "공조난방생산기기"
                                Dim dr() As DataRow = DSET1.tbl_nanbangkiki.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "열생산급탕생산기기"
                                Dim dr() As DataRow = DSET1.tbl_nanbangkiki.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "열생산냉방생산기기"
                                Dim dr() As DataRow = DSET1.tbl_nangbangkiki.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "열생산가습생산기기"
                                Dim dr() As DataRow = DSET1.tbl_nanbangkiki.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "냉난방공조분배시스템"
                                Dim dr() As DataRow = DSET1.tbl_kongjo.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If
                            Case "냉방분배시스템"
                                Dim dr() As DataRow = DSET1.tbl_nbunbae.Select("code='" + data + "'")
                                If dr.Length = 1 Then
                                    data = dr(0)("설명").ToString()
                                End If


                        End Select
                    End If

                    buffer.Append("," + data.Replace(",", ""))
                Next
                buffer.AppendLine()
            Next

            System.IO.File.WriteAllText(fi.FullName, buffer.ToString(), System.Text.Encoding.Default)
        Next
        '  FpSpread1.Visible = True

        If (Not silent) Then
            MsgBox("완료")
            Shell("explorer " + di.FullName, AppWinStyle.NormalFocus)
        End If


    End Sub


    Private Sub Frm_Upload_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

       
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim fd As New FolderBrowserDialog
        fd.SelectedPath = TextBox1.Text
        If (fd.ShowDialog() = Windows.Forms.DialogResult.OK) Then

            Dim files() As String = System.IO.Directory.GetFiles(fd.SelectedPath, "*.tpl")
            For Each filename As String In files

                MdiMain.Open_File(filename, False, False, True)
                MakeData(fd.SelectedPath, True)
            Next

            MsgBox(files.Length.ToString() + " 개 완료")
            Shell("explorer " + fd.SelectedPath, AppWinStyle.NormalFocus)
        End If
    End Sub
End Class