VB,크리스탈레포트

[VB]엑셀 자료 읽기

자료나눔 2011. 1. 20. 17:38

Option Explicit

Private Sub Command1_Click()
Dim FileNameStr         As String
Dim FileNO              As Long
Dim i%, j%
Dim Excelapp            As Object
Dim excelwkb            As Object
Dim cxcelsht            As Object
Dim ActiveSheetInt      As Object

       
        With CommDialog

            .Filter = "모든파일(*.*)|*.*|모든 엑셀파일(*.xl*;xlsx)|*.xl*;xlsx"
            .FilterIndex = 2
            .ShowOpen
           
            FileNameStr = .FileName

           
        End With
           
           
            Set Excelapp = CreateObject("Excel.Application")
            Set excelwkb = Excelapp.Workbooks.Open(FileNameStr)
            Set cxcelsht = excelwkb.Worksheets(1)
            Set ActiveSheetInt = Excelapp.ActiveSheet
       
        With sprFileOpen
            .MaxRows = 0
       
        .MaxRows = ActiveSheetInt.UsedRange.Rows.Count
        .MaxCols = ActiveSheetInt.UsedRange.Columns.Count


            For i = 0 To .MaxRows - 1

                .Row = i + 1
                For j = 1 To .MaxCols

                    .Col = j

                   
                    .Text = Excelapp.Cells(i + 1, j)

                Next j

            Next i

            excelwkb.Close: Excelapp.Quit
            Set cxcelsht = Nothing: Set excelwkb = Nothing: Set Excelapp = Nothing

        End With

End Sub

사용컨트롤 스프레드 : sprFileOpen

커먼 다이얼로그 컨트롤 : CommDialog

커맨드 버튼  : Command1