Reading an Exporting Text Files

.net Guatemala
  • 13 years ago

    I have a text file (.txt) wich i need to read and then export to Excel to get it filtered.  The txt file have 6 columns and up to 28 lines.  I read a similar post and code but it use a database file to get the text from.  I have a txt file.  Any help will be nice.

    Thanks.

  • 13 years ago

    You should use the StreamReader class to read each line of the file.  A google search on how to read a text file or how to use the StreamReader class should provide you with examples.  Once you've read a line in as a string you can use the split method to divide the line into individual columns.  Once you've got the line parsed you would export it to excel.

    See what you can come up with.  If you need any additional help please let me know.

  • 13 years ago

    Well, after my "hello world" first app. on visual basic this a hard challenge.  But I try it out.  I found the way to make a class. (right click on the solution --> add --> class), and I also found an example that use streamreader:

    Class

    Test

    Public Shared Sub Main()

    Try

    ' Create an instance of StreamReader to read from a file.

    Dim sr As StreamReader = New StreamReader("TestFile.txt")

    Dim line As String

    ' Read and display the lines from the file until the end

    ' of the file is reached.

    Do

    line = sr.ReadLine()

    Console.WriteLine(Line)

    Loop Until line Is Nothing

    sr.Close()

    Catch E As Exception

    ' Let the user know what went wrong.

    Console.WriteLine(

    "The file could not be read:")

    Console.WriteLine(E.Message)

    End Try

    End Sub

    It doesn´t define the location of the text file and I don´t understand where it supposed to go (in a class or a module) and how to run this proccess (should I create a button that trigger that event?).

    Finally I found a short guide to create classes that implement the split method that you mentioned here:

    http://msdn2.microsoft.com/en-us/library/ms973814.aspx

    but this work over a text that I input on a textbox ("..The rain in spain....), not over a text file.  How I point to my text file? Well, to many questions I guess.  I will try to read more (if I found the time for it).  This programming hobbie is demanding more time that I thougt.  Anyway, I appreciate your time and your patience.

     

  • 13 years ago
    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.

  • 13 years ago

    Thanks again.  I will try to get it work.  I let you know what happen then...

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”