Solved

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

Posted on 2010-11-21
3
1,445 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 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