0

I had seen many solution to export csv file using either StringBuilder and Foreach. but nobody can describe what will happens when following condition will come?

But my question is what happens when contains comma(,) into field value? I want export data with comma(,) value

Before giving answer please read carefully my question. For more help i giving you an example table as given below.

Example table:

Field-1                Field-2                      Field-3
Dummy data             27 Oct, 2012                 Mark,David,Nancy etc...
Dummy data             27 Oct, 2012                 Mark,David,Nancy etc...
Dummy data             27 Oct, 2012                 Mark,David,Nancy etc...
Dummy data             27 Oct, 2012                 Mark,David,Nancy etc...
Dummy data             27 Oct, 2012                 Mark,David,Nancy etc...
And so on......

I want export above data into .csv file. Also i want export exact value.
Not with double quote e.g "27 Oct, 2012" NOT VALID

7
  • You can use double quotes to surround the values in a field with commas - i.e., "27 Oct, 2012" or "Mark,David,Nancy etc..." Commented Oct 28, 2012 at 7:10
  • Thanks tim for ur reply, but double quotes will display into exported .csv file. Commented Oct 28, 2012 at 7:14
  • 1
    CSV is a very basic format and double-quoting fields with commas is the correct solution for your situation. Is there a reason you don't want the quotes to show in the CSV file? Most CSV readers (if not all) will know how to handle them properly. Commented Oct 28, 2012 at 7:16
  • Yes, because i have one project task its already in php and my task is convert into .net. Client simply ask me if php can do the why .net can't? Commented Oct 28, 2012 at 7:23
  • If you know the values in the field (i.e, you know that Oct 27, 2012 is one field), then you can simply write it out that way. I'm a little confused as to exactly what the problem is - you can write whatever you want to the file. Can you post some of your code? That might make it a little clearer. Commented Oct 28, 2012 at 7:27

1 Answer 1

1

You should enclose fields with commas in double quotes. If the quoted field values contains double quotes, use double double quotes in their place.

In your case:

Dummy data,"27 Oct, 2012","Mark,David,Nancy"
Dummy data,"27 Oct, 2012","Mark,David ""the CSV wiz"",Nancy"

Also, web search.

EDIT: after your comment against double quotes, you'll have to parse it manually and take the known comma positions into account, but that will only work if there is only one field with an arbitrary number of commas and it comes last (or you parse the data from both sides and take the remainder as that field).

If you have no way of deterministic comma parsing, then use another delimiter, like the Tab. Something that cannot be in the input, basically.

Sign up to request clarification or add additional context in comments.

2 Comments

No, i want display exact value. Double quotes will display into .csv file
If you display exact values, then any CSV reader will assume that each comma denotes a new field. Why are you against double quotes being in the CSV file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.