Retaining a Comma when writing to a CSV

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a console app in c# wherein am converting a dataset into a CSV
file. It works fine. But I have some values in the dataset which have a comma
within(eg. A,B,C). When I view the CSV file in Excel, the multiple values
which are separated by the 'comma' in the dataset are being displayed in the
next column , which I dont want that way. I want to retain any commas which
are present in my dataset.
How do I handle such a scenario?

Thanks
Deepa
 
You could do a couple things. If you put quotes around your text (called
text qualifier), "A,B,C" then Excel will ignore the commas within the
quotes. Another choice is to use a deliminator other than a comma, such as
a TAB or a pipe.

Kevin
 
Kevin
I tried using the tab and the pipe delimiters but the resulting output file
when viewed in excel isnt the same as i want it.(similar to a csv).
I thought of the double quotes but am not able to figure out how I write the
code to see what values in the dataset have a comma or not.
Would appreciate a response to this.

Thanks
Deepa
 
You don't have to worry about which values have a comma or not. If the
field is a text field (or you want it treated as such by Excel) just enclose
it in quotes. You might have a row that ends up looking like:

"abcdef", "23klk,23,23", "eeeejjj","eee,t,r", 9,5

So you see, you put quotes around all your text fields. If one ends up with
a comma in it, Excel will ignore it because it's in quotes.

Kev
 
Thanks dude. Just curious if you can help me with something else.
I have this dataset am converting into a csv file. Before I write to the
output.csv I want to sort the values of the dataset in ascending order. I
just have one single table in my dataset. I have the dataset ready, i mean to
say am not making any connection to the database to populate the dataset. I
have it as an xml dataset file. And am just saying ds.ReadXml(the xml file
path)
Before I loop thru the dataset, I want to sort it.
Any ideas?

thanks
Deepa
 
I am writing a console app in c# wherein am converting a dataset into a CSV
file. It works fine. But I have some values in the dataset which have a comma
within(eg. A,B,C). When I view the CSV file in Excel, the multiple values
which are separated by the 'comma' in the dataset are being displayed in the
next column , which I dont want that way. I want to retain any commas which
are present in my dataset.
How do I handle such a scenario?

Thanks
Deepa

For all you ever wanted to know about CSV files, have a look at
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm

rossum
 
You forgot to mention what to do when there's quotes in the data. You'd
need to double them up as an escape.

The other option is to use my csv parser,
http://www.geocities.com/shriop/index.html , or one like it, which
handles all of this for you and can write all data directly from a
DataTable.
 
Thanks Kevin,will try doing that

Kevin Thomas said:
Well if your DataTable is in a Dataset with no other tables, then it would
look something like this:

myDataset.tables[0].DefaultView.Sort = "Name Of Column You Want To Sort By"

See if that works for ya.

See this article for more info:
http://msdn.microsoft.com/library/d...fsystemdatadatatableclassdefaultviewtopic.asp

Kevin

Deepa said:
Thanks dude. Just curious if you can help me with something else.
I have this dataset am converting into a csv file. Before I write to the
output.csv I want to sort the values of the dataset in ascending order. I
just have one single table in my dataset. I have the dataset ready, i mean
to
say am not making any connection to the database to populate the dataset.
I
have it as an xml dataset file. And am just saying ds.ReadXml(the xml file
path)
Before I loop thru the dataset, I want to sort it.
Any ideas?

thanks
Deepa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top