?
Solved

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

Posted on 2010-11-21
3
Medium Priority
?
1,761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

752 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