Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm just new in programming and I want to know how will I put the values listed in my List view to a csv file.

Thanks
Posted
Updated 7-Jul-23 10:17am

Use below algorithm and code accordingly
VB
Variable : StrCSVFiles
StrCSVFiles = ""

Loop through all list item (increment x as index)
   StrCSVFiles = StrCSVFiles + Listview1[x].listitem(0) + "," + Listview1[x].listitem(1)
 x = x +1
End Loop

 Create file
 File.Write (StrCSVFiles)
 
Share this answer
 
v2
Public Sub ExportListviewToCsv(ByRef theListView As ListView, csvExportFileName As String, Optional writeHeader As Boolean = True, Optional useQuotes As Boolean = True)
    Dim fileNum As Integer
    Dim theListItem As ListItem
    Dim idx As Long
    
    fileNum = FreeFile(255)
    
    Open csvExportFileName For Output As #fileNum
    
    If writeHeader = True Then
        For idx = 1 To theListView.ColumnHeaders.Count
            If useQuotes = True Then
            Print #fileNum, Chr(34) & theListView.ColumnHeaders(idx).Text & Chr(34);
            Else
                Print #fileNum, theListView.ColumnHeaders(idx).Text;
            End If
            If idx < theListView.ColumnHeaders.Count Then
                Print #fileNum, ",";
            End If
        Next
        Print #fileNum, ""
    End If
    
    For Each theListItem In theListView.ListItems
        If useQuotes = True Then
            Print #fileNum, Chr(34) & theListItem.Text & Chr(34);
        Else
            Print #fileNum, theListItem.Text;
        End If
        If theListItem.ListSubItems.Count > 0 Then
            Print #fileNum, ",";
            For idx = 1 To theListItem.ListSubItems.Count
                If useQuotes = True Then
                    Print #fileNum, Chr(34) & theListItem.SubItems(idx) & Chr(34);
                Else
                    Print #fileNum, theListItem.SubItems(idx);
                End If
                If idx < theListItem.ListSubItems.Count Then
                    Print #fileNum, ",";
                End If
            Next
        End If
        Print #fileNum, ""
    Next
    
    Close #fileNum
    
End Sub
 
Share this answer
 
Comments
Richard Deeming 10-Jul-23 4:16am    
An unexplained code-dump is not a good solution to any question - and particularly not to one from 11½ years ago!

Stick to answering new questions unless you have something new and interesting to add. And make sure to explain yourself, rather than just dumping a wall of code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900