CSV in Array einlesen

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von SystemUnknow.

    CSV in Array einlesen

    Hallo zusammen,

    ich möchte eine CSV Datei von Visual Basic 2010 einlesen lassen, dazu habe ich folgenden Code.

    Leider habe ich das Problem das folgende Fehlermeldung erscheint:

    Eine Ausnahme (erste Chance) des Typs "System.IndexOutOfRangeException" ist in WindowsApplication1.exe aufgetreten.

    Und zwar in Zeile 33 - wo liegt hier der Fehler, steige gerade nicht durch :(

    VB.NET-Quellcode

    1. Private Sub ReadCSVFileToArray()
    2. Dim strfilename As String
    3. Dim num_rows As Long
    4. Dim num_cols As Long
    5. Dim x As Integer
    6. Dim y As Integer
    7. Dim strarray(1, 1) As String
    8. ' Load File
    9. strfilename = "C:\Daten\test.csv"
    10. 'Check if file exist
    11. If File.Exists(strfilename) Then
    12. Dim tmpstream As StreamReader = File.OpenText(strfilename)
    13. Dim strlines() As String
    14. Dim strline() As String
    15. ' Load content of file to strlines array
    16. strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)
    17. ' Redimension the array.
    18. num_rows = UBound(strlines)
    19. strline = strlines(0).Split(",")
    20. num_cols = UBound(strline)
    21. ReDim strarray(num_rows, num_cols)
    22. ' Copy the data into the array.
    23. For x = 0 To num_rows
    24. strline = strlines(x).Split(",")
    25. For y = 0 To num_cols
    26. strarray(x, y) = strline(y)
    27. Next
    28. Next
    29. ' Display the data in textbox
    30. For x = 0 To num_rows
    31. For y = 0 To num_cols
    32. TextBox1.Text = TextBox1.Text & strarray(x, y) & ","
    33. Next
    34. TextBox1.Text = TextBox1.Text & Environment.NewLine
    35. Next
    36. End If
    37. End Sub
    ungetestet, sollte aber die Lösung sein...
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub ReadCSVFileToArray()
    2. Dim strfilename As String
    3. Dim num_rows As Long
    4. Dim num_cols As Long
    5. Dim x As Integer
    6. Dim y As Integer
    7. Dim strarray(1, 1) As String
    8. ' Load File
    9. strfilename = "C:\Daten\test.csv"
    10. 'Check if file exist
    11. If File.Exists(strfilename) Then
    12. Dim tmpstream As StreamReader = File.OpenText(strfilename)
    13. Dim strlines() As String
    14. Dim strline() As String
    15. ' Load content of file to strlines array
    16. strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)
    17. ' Redimension the array.
    18. num_rows = UBound(strlines)
    19. strline = strlines(0).Split(",")
    20. num_cols = UBound(strline)
    21. ReDim strarray(num_rows, num_cols)
    22. ' Copy the data into the array.
    23. For x = 0 To num_rows-1
    24. strline = strlines(x).Split(",")
    25. For y = 0 To num_cols-1
    26. strarray(x, y) = strline(y)
    27. Next
    28. Next
    29. ' Display the data in textbox
    30. For x = 0 To num_rows-1
    31. For y = 0 To num_cols-1
    32. TextBox1.Text = TextBox1.Text & strarray(x, y) & ","
    33. Next
    34. TextBox1.Text = TextBox1.Text & Environment.NewLine
    35. Next
    36. End If
    37. End Sub