954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array to CSV File

Hello,

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.

Thanks.

crazycode300
Newbie Poster
3 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

lolafuertes
Master Poster
798 posts since Oct 2008
Reputation Points: 120
Solved Threads: 167
 

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?

thanks.

crazycode300
Newbie Poster
3 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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.

lolafuertes
Master Poster
798 posts since Oct 2008
Reputation Points: 120
Solved Threads: 167
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: