Solved

How To Read Excel And Load Data To Listview in VB6?

Posted on 2010-11-21
3
1,494 Views
Last Modified: 2012-05-10
Hello I've been searching the forums about how to read an excel file and load the data into a listview. What I got from the forum is how to load data from a listview into excel.

What I have in mind is to select an excel file using a common dialog control and loads the data into a listview.

0
Comment
Question by:Fi-es
3 Comments
 
LVL 5

Expert Comment

by:SinghAmandeep
ID: 34186547

First Perform uploading part :

Dim xlApp As Excel.Application
Dim Wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set xlApp = New Excel.Application
Set Wb = xlApp.Workbooks.Open("C:\sample.xls")

Set ws = Wb.Sheets(1)
Msgbox ws.Cells(1, 3)

**************** Then Use this to Load data to listview in VB6

Public Sub Load(mPath As String, LVW As ListView)
    Dim FSO As FileSystemObject, TS As TextStream, strLine as String,  tabLine() As String, lItem As ListItem, j As Integer
   
    On Error Resume Next
    If Len(Dir(mPath)) > 0 Then
        Dim tabReplace(4)
        tabReplace(2) = "name": tabReplace(3) = "phone": tabReplace(4) = "Good"
       
        Set FSO = New FileSystemObject
        Set TS = FSO.OpenTextFile(mPath, ForReading, False)
        While Not TS.AtEndOfStream
          strLine = TS.ReadLine
          if strLine <> empty then
            tabLine = Split(strLine, ":")
            Set lItem = LVW.ListItems.Add(, , tabLine(0))
            For j = 1 To UBound(tabLine)
                lItem.ListSubItems.Add , , tabLine(j)
            Next j
            For j = UBound(tabLine) + 1 To 4
                lItem.ListSubItems.Add , , tabReplace(j)
            Next j
          end if
        Wend
        TS.Close
        Set TS = Nothing
        Set FSO = Nothing
    End If
End Sub
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 34186819
You could do it like this:
Private Sub Command1_Click()

    Dim xlApp As Excel.Application

    Dim xlWbk As Excel.Workbook

    Dim xlWks As Excel.Worksheet

    Dim c As Integer

    Dim r As Integer

    Dim strItem As String

    

    With CommonDialog1

        .Filter = "Excel (*.xls*)|*.xls*"

        .ShowOpen

        On Error Resume Next

            Set xlApp = GetObject(, "Excel.Application")

        On Error GoTo 0

        If xlApp Is Nothing Then

            Set xlApp = CreateObject("Excel.Application")

        End If

        xlApp.Visible = True

        Set xlWbk = xlApp.Workbooks.Open(.FileName)

    End With

    

    Set xlWks = xlWbk.sheets(1)

    c = 1

    r = 1

    strItem = xlWks.Cells(r, 1).Value

    Do Until strItem = ""

        With ListView1

            .View = lvwList

            .ListItems.Add , , strItem

            r = r + 1

            strItem = xlWks.Cells(r, 1).Value

        End With

    Loop

End Sub

Open in new window

0
 

Author Closing Comment

by:Fi-es
ID: 34220880
The solution you given is working fine. Thank you for your support
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now