Sign in to follow this  
Followers 0
Knight

CSV to Treeview

2 posts in this topic

This script will read a .csv file and put it in a treeview based off the second column of the document.

It will read the second column of the file and sort it under the right branch in the treeview. I needed this for my current project, hope someone finds it helpful ;)

#include <GuiConstants.au3>
#include <File.au3>

GuiCreate("CSV to Treeview Test", 200, 340,(@DesktopWidth-400)/2, (@DesktopHeight-400)/2)

$Label = GuiCtrlCreateLabel("CSV to Treeview Test", 10, 10, 170, 30, $SS_CENTER)
$Tree = GuiCtrlCreateTreeview(10, 50, 180, 280)
$1 = GUICtrlCreateTreeViewItem("Head1", $tree)
$2 = GUICtrlCreateTreeViewItem("Head2", $tree)

$in_filename = "file.csv"
Dim $lines, $Display, $NumCols
_FileReadToArray($in_filename, $lines)
$Columns = StringSplit($lines[1], ",")
$NumCols = $Columns[0]
Dim $array[ $lines[0] ][ $Columns[0] ]
For $i = 1 To $lines[0]
    $Columns = StringSplit($lines[$i], ",")
    If $Columns[0] = 1 Then ContinueLoop
    For $j = 1 To $Columns[0]
        $array[$i - 1][$j - 1] = $Columns[$j]
    Next
Next

$t = 0
While 1
    If $t < UBound($Array, 1) Then
        If $Array[$t][1] = 1 Then
            GUICtrlCreateTreeViewItem($Array[$t][0], $1)
        ElseIf $Array[$t][1] = 2 Then
            GUICtrlCreateTreeViewItem($Array[$t][0], $2)
        EndIf
        $t += 1
        ContinueLoop
    Else
        ExitLoop
    EndIf
WEnD

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
    ;;;
    EndSelect
WEnd

This is the file.csv

Item1,1 
Item2,1 
Item3,1 
Item4,2 
Item5,2 
Item6,2
Item7,2
Item8,2
Item9,2

Some of the csv reading code was taken and modified from a post that I was unable to locate; and I modified it to work for me.

Share this post


Link to post
Share on other sites



Interesting. I will have to try it on an exported Excel file to see how good it will read the CSV. Your test data worked great.

Cheers.. ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0