By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

Retaining a Comma when writing to a CSV

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
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

Jul 21 '05 #2

P: n/a
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

"Kevin Thomas" wrote:
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
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


Jul 21 '05 #3

P: n/a
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
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

"Kevin Thomas" wrote:
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
>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


Jul 21 '05 #4

P: n/a
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

"Kevin Thomas" wrote:
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
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

"Kevin Thomas" wrote:
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
>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


Jul 21 '05 #5

P: n/a
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/de...tviewtopic.asp

Kevin

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:FB**********************************@microsof t.com...
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

"Kevin Thomas" wrote:
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
> 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
>
> "Kevin Thomas" wrote:
>
>> 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
>>
>> "Deepa" <De***@discussions.microsoft.com> wrote in message
>> news:16**********************************@microsof t.com...
>> >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
>>
>>
>>


Jul 21 '05 #6

P: n/a
On Fri, 11 Mar 2005 12:03:04 -0800, "Deepa"
<De***@discussions.microsoft.com> wrote:
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
--

The ultimate truth is that there is no Ultimate Truth
Jul 21 '05 #7

P: n/a
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.

Jul 21 '05 #8

P: n/a
Thanks Kevin,will try doing that

"Kevin Thomas" wrote:
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/de...tviewtopic.asp

Kevin

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:FB**********************************@microsof t.com...
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

"Kevin Thomas" wrote:
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

"Deepa" <De***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
> 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
>
> "Kevin Thomas" wrote:
>
>> 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
>>
>> "Deepa" <De***@discussions.microsoft.com> wrote in message
>> news:16**********************************@microsof t.com...
>> >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
>>
>>
>>


Jul 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.