Global It Leader!!



 
 

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

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 9,305회 작성일 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 오피스팁에서 이동 됨]

댓글목록

등록된 댓글이 없습니다.

전체 95
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
35 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 10036 07-15
34 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8109 07-14
33 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7420 07-14
32 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8897 07-13
31 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8962 07-13
30 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7567 07-08
29 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7250 07-08
28 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8597 07-08
27 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8090 07-08
26 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7887 07-08
25 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8249 07-08
열람중 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9306 07-05
23 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8799 07-04
22 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8872 07-02
21 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8091 07-01
20 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4865 10-29
19 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5319 08-31
18 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 10433 08-31
17 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5278 08-31
16 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6574 08-31