Imports Excel = Microsoft.Office.Interop.Excel

Module ExcelModule
    Private xlApp As New Excel.Application  '엑셀 응용프로그램의 변수.
    Private xlworkbook As Excel.Workbook
    Private xlworksheet As Excel.Worksheet
    Private xlRange As Excel.Range
    Public xlReadData(,) As Object

    Private Function OpenDBFile() As String
        Dim filePath As String

        With frmMySqlTest.OpenFileDB
            .Title = "[JION MEDITECH] Select T/D DB File!"
            .InitialDirectory = "d:\"
            .Filter = "T/D DB Files | *.xlsx;*.xls;"
            .Multiselect = False
            If .ShowDialog() = 0 Then
                .ShowDialog()
            End If
        End With

        filePath = frmMySqlTest.OpenFileDB.FileName
        Return filePath

    End Function

    Public Function loadExcelDB() As Boolean
        Dim xlRowCnt, xlCollumnCnt As Integer
        Dim xlEndPosition As String
        Dim currRowCnt As Integer
        Dim filePath As String

        Try
            filePath = OpenDBFile()
            'filePath = "d:\Excel_Spl.xlsx"

            '엑셀 창을 띄울것인지 여부 체크
            xlApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
            xlApp.Visible = False
            xlworkbook = xlApp.Workbooks.Open(filePath)
            xlworksheet = xlworkbook.Sheets(1)

            'xlRange = xlworksheet.Range("A1", Type.Missing)
            xlRowCnt = xlworksheet.Range("A1").CurrentRegion.Rows.Count
            xlCollumnCnt = xlworksheet.Range("A1").CurrentRegion.Columns.Count
            xlEndPosition = xlworksheet.Range("A1").CurrentRegion.Address

            xlRange = xlworksheet.Range(xlEndPosition)

            'Retrieve the data from the range.
            ReDim xlReadData(xlRowCnt, xlCollumnCnt)
            xlReadData = xlRange.Value

            xlworkbook.Close()
            xlApp.Quit()

            currRowCnt = frmMySqlTest.DataGridView.Rows.Count

            If currRowCnt > 1 Then
                For cnt = 1 To currRowCnt - 1
                    frmMySqlTest.DataGridView.Rows.RemoveAt(0)
                Next
            End If

            frmMySqlTest.txbPVdate.Text = xlReadData(2, 2)

            For cnt = 4 To xlRowCnt
                frmMySqlTest.DataGridView.Rows.Add(xlReadData(cnt, 1), xlReadData(cnt, 2), Val(xlReadData(cnt, 3)) * 1000, xlReadData(cnt, 4), xlReadData(cnt, 5), xlReadData(cnt, 6), xlReadData(cnt, 7))
            Next

            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            xlworkbook.Close()
            xlApp.Quit()
            Return False
        End Try


    End Function

    Public Function loadIQCExcelDB() As Boolean
        Dim xlRowCnt, xlCollumnCnt As Integer
        Dim xlEndPosition As String
        Dim currRowCnt As Integer
        Dim filePath As String

        Try
            filePath = OpenDBFile()
            'filePath = "d:\Excel_Spl.xlsx"

            '엑셀 창을 띄울것인지 여부 체크
            xlApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
            xlApp.Visible = False
            xlworkbook = xlApp.Workbooks.Open(filePath)
            xlworksheet = xlworkbook.Sheets(1)

            'xlRange = xlworksheet.Range("A1", Type.Missing)
            xlRowCnt = xlworksheet.Range("A1").CurrentRegion.Rows.Count
            xlCollumnCnt = xlworksheet.Range("A1").CurrentRegion.Columns.Count
            xlEndPosition = xlworksheet.Range("A1").CurrentRegion.Address

            xlRange = xlworksheet.Range(xlEndPosition)

            'Retrieve the data from the range.
            ReDim xlReadData(xlRowCnt, xlCollumnCnt)
            xlReadData = xlRange.Value

            xlworkbook.Close()
            xlApp.Quit()

            currRowCnt = frmMySqlTest.DataGridView.Rows.Count

            If currRowCnt > 1 Then
                For cnt = 1 To currRowCnt - 1
                    frmMySqlTest.DataGridView.Rows.RemoveAt(0)
                Next
            End If

            For cnt = 2 To xlRowCnt
                frmMySqlTest.DataGridView.Rows.Add(xlReadData(cnt, 1), xlReadData(cnt, 2), xlReadData(cnt, 3), xlReadData(cnt, 4), xlReadData(cnt, 5), xlReadData(cnt, 6), Format(xlReadData(cnt, 7), "0.000"), Format(xlReadData(cnt, 8), "0.000"), Format(xlReadData(cnt, 9), "0.000"), Format(xlReadData(cnt, 10), "0.000"), Format(xlReadData(cnt, 11), "0.000"), Format(xlReadData(cnt, 12), "0.000"))
            Next


            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            xlworkbook.Close()
            xlApp.Quit()
            Return False
        End Try


    End Function


End Module
