Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read an Excel file(97-03) in Visual Basic 6.0

Tags:

vb6

Can anybody tell me how to read an Excel file in visual basic 6.0 and import all the values into a listview or datagridview,want to use a simple and efficient technique to achieve this. can anyone help me to solve this

like image 432
karthik Avatar asked May 02 '12 13:05

karthik


1 Answers

This should import data from an Excel file into a ListView:

    Dim ExcelObj As Object
    Dim ExcelBook As Object
    Dim ExcelSheet As Object
    Dim i As Integer

    Set ExcelObj = CreateObject("Excel.Application")
    Set ExcelSheet = CreateObject("Excel.Sheet")

    ExcelObj.WorkBooks.Open App.Path & "\ExcelFile.xls"

    Set ExcelBook = ExcelObj.WorkBooks(1)
    Set ExcelSheet = ExcelBook.WorkSheets(1)

    Dim l As ListItem
    lvwList.ListItems.Clear
    With ExcelSheet
    i = 1
    Do Until .cells(i, 1) & "" = ""
        Set l = lvwList.ListItems.Add(, , .cells(i, 1))
        l.SubItems(1) = .cells(i, 2)
        l.SubItems(2) = .cells(i, 3)
        l.SubItems(3) = .cells(i, 4)
        i = i + 1
    Loop

    End With

    ExcelObj.WorkBooks.Close

    Set ExcelSheet = Nothing
    Set ExcelBook = Nothing
    Set ExcelObj = Nothing
like image 156
Xaisoft Avatar answered Sep 28 '22 10:09

Xaisoft