Solved

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

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

726 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