Question Extracting data from textfile or excel spreadsheet with multiple columns

jtham

New member
Joined
Mar 30, 2010
Messages
3
Programming Experience
Beginner
Hi, i am a beginner at programming and i need help in reading data, currently i need to use 4 different texts files in order to read data and run my program, is there any way that i can just use 1 text file or 1 excel spreadsheet with multiple columns? Help please, here are my codes now.

VB.NET:
Dim File1 As New System.IO.StreamReader("C:\Documents and Settings\User\Desktop\WindDirectionLanding.txt") 'Calling out the file.
        Dim File2 As New System.IO.StreamReader("C:\Documents and Settings\User\Desktop\WindSpeedLanding.txt") 'Calling out the file.
        Dim Line1 As String = File1.ReadLine() 'Reading the file.
        Dim Line2 As String = File2.ReadLine() 'Reading the file.

        Do Until Line1 Is Nothing OrElse Line2 Is Nothing

            Dim WindDirectionLanding As Integer = CInt(Line1) 'Converting the data in text file from string to integer.
            Dim WindLanding As Integer = CInt(Line2) 'Converting the data in text file from string to integer.

            R = WindLanding * 6 'Multiplying 6 due to error when plotting the graph.
            X = Math.Cos(WindDirectionLanding * PI / 180) * R 'Converting Polar coordinates to Cartesian coordinates.
            Y = Math.Sin(WindDirectionLanding * PI / 180) * R 'Converting Polar coordinates to Cartesian coordinates.

            With New Point(360, 360) 'To rotate the points about the origin (360,360).

                Rx = Math.Cos(270 * PI / 180) * X - Math.Sin(270 * PI / 180) * Y
                Ry = Math.Sin(270 * PI / 180) * X + Math.Cos(270 * PI / 180) * Y
                Px = Rx + 360 'Adding 360 for offsetting along the x-axis.
                Py = Ry + 360 'Adding 360 for offsetting along the y-axis.

                e.Graphics.FillEllipse(Brushes.Blue, Px - 3, Py - 3, 6, 6) 'Subtracting 5 to offset the filled circle along the x and y axis.

            End With


            Line1 = File1.ReadLine() 'To read the next line.
            Line2 = File2.ReadLine() 'To read the next line.

        Loop

        File1.Close() 'To close the file.
        File2.Close() 'To close the file.

        'Plotting the data for TakeOff.

        Dim File3 As New System.IO.StreamReader("C:\Documents and Settings\User\Desktop\WindDirectionTakeOff.txt") 'Calling out the file.
        Dim File4 As New System.IO.StreamReader("C:\Documents and Settings\User\Desktop\WindSpeedTakeOff.txt") 'Calling out the file.
        Dim Line3 As String = File3.ReadLine() 'Reading the file.
        Dim Line4 As String = File4.ReadLine() 'Reading the file.

        Do Until Line3 Is Nothing OrElse Line4 Is Nothing

            Dim WindDirectionTakeOff As Integer = CInt(Line3) 'Converting the data in text file from string to integer.
            Dim WindTakeOff As Integer = CInt(Line4) 'Converting the data in text file from string to integer.

            R = WindTakeOff * 6 'Multiplying 6 due to error when plotting the graph.
            X = Math.Cos(WindDirectionTakeOff * PI / 180) * R 'Converting Polar coordinates to Cartesian coordinates.
            Y = Math.Sin(WindDirectionTakeOff * PI / 180) * R 'Converting Polar coordinates to Cartesian coordinates.

            With New Point(360, 360) 'To rotate the points about the origin (360,360).

                Rx = Math.Cos(270 * PI / 180) * X - Math.Sin(270 * PI / 180) * Y
                Ry = Math.Sin(270 * PI / 180) * X + Math.Cos(270 * PI / 180) * Y
                Px = Rx + 360 'Adding 360 for offsetting along the x-axis.
                Py = Ry + 360 'Adding 360 for offsetting along the y-axis.

                e.Graphics.FillEllipse(Brushes.DeepPink, Px - 3, Py - 3, 6, 6) 'Subtracting 5 to offset the filled circle along the x and y axis.

            End With

            Line3 = File3.ReadLine() 'To read the next line.
            Line4 = File4.ReadLine() 'To read the next line.

        Loop

        File3.Close() 'To close the file.
        File4.Close() 'To close the file.

All my 4 text files are in 1 column each.
I have attached my text files

Please help, thanks
 

Attachments

  • WindDirectionLanding.txt
    72 bytes · Views: 24
  • WindSpeedLanding.txt
    38 bytes · Views: 25
  • WindDirectionTakeOff.txt
    46 bytes · Views: 26
  • WindSpeedTakeOff.txt
    42 bytes · Views: 24
Sure: As long as each column will be the same length you could use a DataTable. Add each file into an Excel spread column. Save as .csv file.
Then read the file like:
VB.NET:
Dim dt As New DataTable 'Class level so you can access it anywhere

'the table must the correct number of columns
For i As Integer = 1 To 4 
    dt.Columns.Add("")
Next
Using sr As New StreamReader("yourfilepath.csv")
  While Not sr.EndOfStream
      dt.Rows.Add(sr.ReadLine.Split(","))
  End While
End Using
The DataTable now looks exactly like your file. Now you can loop thru the columns/rows to get your info.
 
Back
Top