Global It Leader!!



 
 

VB 추천! 엑셀파일 불러와 그리드에 뿌리기

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 9,306회 작성일 10-07-05 00:12

본문

Option Explicit

Private Sub Command1_Click()

    Call Load_ExcelFile("C:\aaa.xls", MSFlexGrid1, 1, 1)

End Sub

Public Sub Load_ExcelFile(ByVal strFileName As String, _
                          ByVal grdList As Control, _
                          Optional intStartExcelCol As Integer = 1, _
                          Optional intEndExcelCol As Integer = 10)

    '----------------------------------------------------------------------------------------


    ' 내용 : 엑셀파일을 읽어 그리드에 삽입하는 모듈


    ' 비고 :


    '        strFileName - 엑셀파일의 경로 + 파일명


    '        grdList - 엑셀의 내용을 삽입할 그리드


    '        intStartExcelCol - 엑셀파일에서 사용할 시작 열의 번호 (1  이면 A1 부터 읽는다.)


    '        intEndExcelCol - 엑셀파일에서 사용할 마지막 열의 번호 (10 이면 J1 까지 읽는다.)


    '----------------------------------------------------------------------------------------


    Dim objExcelSheet As Object   '요렇게 오브젝트개체로 선언하면 참조하지 않아도 되니 편함니다.


    Dim objExcelApp As Object


    Dim strData As String


    


    Dim intGrdRow As Integer


    Dim intExcelRow As Integer


    Dim intExcelCol As Integer


    


    Dim intStartCol As Integer


    Dim intEndCol As Integer


    


    On Error GoTo Err


    


    '파일이 존재하는지 검사합니다.


    If Dir(strFileName) = "" Then


        MsgBox strFileName & vbCrLf & " 파일이 존재하지 않습니다.", vbExclamation, "파일 찾기"


        Exit Sub


    End If


    


    Me.MousePointer = vbHourglass


    


    Set objExcelSheet = GetObject(strFileName)


    objExcelSheet.Parent.Windows(1).Visible = True


    


    Set objExcelApp = objExcelSheet.Application


    Set objExcelSheet = objExcelApp.ActiveSheet


  


    '엑셀의 A1에 해당하는 컬럼이 65임


    intStartCol = 64 + intStartExcelCol


    intEndCol = 64 + intEndExcelCol


  


    Do


        intGrdRow = intGrdRow + 1


        intExcelRow = intExcelRow + 1


        


        '첫번째 열만 뽑아내어 엑샐화일 끝인가 확인 한다.


        strData = objExcelSheet.Range(Chr(65) & intExcelRow)


        Select Case strData


        Case ""


            Exit Do


        End Select


        


        '********************************************************************************************


        ' 이 부분에서 읽어온 엑셀파일의 내용을 DB 또는 그리드에 넣는 작업을 하면 됩니다.


        ' 아래소스는 그리드에 넣는 예


        With grdList


            If Val(.Tag) = 0 Then


                '그리드 초기화


                .Clear


                .Cols = intEndExcelCol - intStartExcelCol + 1


                .Rows = 2


                .Tag = "1"


            End If


            


            If .Rows <= intGrdRow Then .Rows = .Rows + 1


            


            For intExcelCol = intStartCol To intEndCol


                strData = objExcelSheet.Range(Chr(intExcelCol) & intExcelRow) 'A1, A2 형태로 만든다.


            


                .TextMatrix(intGrdRow, (intExcelCol - intStartCol)) = strData


            Next intExcelCol


        End With


        '********************************************************************************************


    Loop


   


    'Excel 종료


    objExcelApp.ActiveWorkbook.Saved = True


    objExcelApp.ActiveWorkbook.Close


    objExcelApp.Quit


 

    Set objExcelApp = Nothing


    Set objExcelSheet = Nothing


    


    Me.MousePointer = vbDefault


    


    Exit Sub


Err:


    Me.MousePointer = vbDefault


    MsgBox Err.Description, vbCritical, "엑셀 불러오기"


End Sub

[이 게시물은 오원장님에 의해 2013-02-28 16:42:18 오피스팁에서 이동 됨]

댓글목록

등록된 댓글이 없습니다.

전체 440
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
200 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8250 07-08
열람중 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9307 07-05
198 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8799 07-04
197 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8873 07-02
196 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8092 07-01
195 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4866 10-29
194 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5320 08-31
193 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 10433 08-31
192 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5279 08-31
191 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6575 08-31
190 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7000 08-29
189 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4827 08-29
188 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5711 08-24
187 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5148 08-09
186 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6008 08-09
185 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5576 04-07
184 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 13227 01-07
183 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7347 12-01
182 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7477 04-08
181 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9552 04-08