I prefer to write the loop like this:
Do While Not sr.EndOfStream
' Your code here
Loop
This is because it will execute the code inside the loop only if there is something to read. The file that will be read comes from the hard drive and you need to pass the full path of the file. "TestFile.txt" won't cut it. It should be something like "c:\TestFile.txt". I assume the file to be read will be given by the user. Your program needs to allow the user to enter a file and your program should use whatever the enter. It could be as simple as having a user enter the path in a textbox. Or if it's a console application you might prompt them for the path.
The actual code might go in a procedure or in a button click event or some other appropriate event. If you put the code in a procedure you would then call the procedure at the appropriate time, like when a button is clicked or when a particular event occurs.
The example you found for the split function is really just an example of creating a class. So don't worry to much about most of it. The important information is the Split function. Although I wish the example hadn't used the old vb6 function. I'll show you the .net way in a second. The idea is to read a line of text from your file and then split it into parts. You've got the code to read each line of text. Now you just need to split that text. The new do loop might look something like this.
Do While Not sr.EndOfStream
' Get a line of text
Dim line As String = sr.ReadLine
' Split the line into columns (The ',' is the character that seperates each column. Yours may be different.)
' Columns is a string array. It will hold each columns value.
Dim columns As String() = line.Split(",")
' Display the full line as it appears in the textfile
Console.WriteLine(line)
' Show each individual column's value
For i As Integer = 0 To columns.Length - 1
Console.WriteLine("Column {0}: {1}", i, columns(i))
Next
Loop
Notice in my code I use line.Split. The variable 'line' is a string. The string class provides many methods for working with strings. The 'split' method is just one of them.
In my example I have outputed each column to the console window. In your case you would want to add them to the excel file. That parts up to you. I don't know anything about writing to excel files.
Enter your message below
Sign in or Join us (it's free).