I have data imported from a text file stored in an array. How do I go about writing it to a CSV file? Also, how do I set up different columns so that the CSV file opens up nicely in Excel.


Recommended Answers

All 3 Replies

A .csv file is just a text file with the column data being separated by the list separator character defined in your control panel, regional and language options.

Many places define it as the semicolon character ( ; ).

Usually the very first row written in the csv file, contains the column title, but is not required. All the data written in the file is text, so numeric values sould be formatted using the right format. You can use the System.Globalization.CurrentUICulture to obtain the right formatting characters for you.

Is recommended to have the file UTF encoded in order to support 2 byte string characters for languages others than English.

You can use a TextWriter or an StreamWriter for those purposes.

Hope this helps

that definitely helps. just one more question. do you know how I can count the number of tabs at the beginning of a line in a text file?


If you read the line in a string array, you can cicle over the string.Chars array to coun them using some thing like:

' This is an untested code
' Also is assumming you are using the Microsoft.VisualBasic namespace
Dim TabsCount as Integer = 0 ' this will return the number of tabs at the beginning
Dim CharacterPosition as Integer = 0 ' this will be the pointer to the character array
While CharacterPosition < InputLine.Length And InputLine.Chars(CharacterPosition) = ControlChars.Tab ' this will stop counting if you reach the end of line or a character other than tab
    TabsCount +=1 ' increase the counter
End While

Hope this helps. If this solved your question, please be so kind to mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.