31

I am using a data table for storing data.

I am exporting the data from data table to CSV file.

Sometimes there may be values containing comma(,) so the values are not exported correctly.

For Example

Consider the value is "9,11,32". I have to export as such.

But when I do the first column conatins 9 then in next column 11.

I want to display 9,11,32 in the same column in the CSV file. How do I do that?

Liam
  • 27,717
  • 28
  • 128
  • 190
user1027129
  • 341
  • 2
  • 4
  • 4
  • possible duplicate of [Dealing with commas in a CSV file](http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – slugster Nov 13 '11 at 09:16
  • What does "**not exported correctly." mean? Normally you put the data in quotes but it depends on what is _reading_ the file, not on what is writing it. I always suggest you use tab delimited instead then you don't have to worry about commas. – Nick.Mc Jan 04 '18 at 01:45
  • Possible duplicate of [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – Liam Jul 11 '19 at 13:39

6 Answers6

32

Simply put your data inside the backslash like this: "\"" + yourdata + "\"". Take a look on the example below:

StringWriter csv = new StringWriter();
// Generate header of the CSV file
csv.WriteLine(string.Format("{0},{1}", "Header 1", "Header 2"));
// Generate content of the CSV file
foreach (var item in YourListData)
{
    csv.WriteLine(string.Format("{0},{1}", item.Data1, "\"" + item.Data2 + "\""));
}

return File(new System.Text.UTF8Encoding().GetBytes(csv.ToString()), "application/csv", string.Format("{0}{1}", "YourFileName", ".csv"));

In the example: Your data2 may contains comma ","

Anh Hoang
  • 2,242
  • 3
  • 22
  • 23
  • But if My data is like abc,def" then it does not works as you explain. it give one column with abc,def" and second column with value def"". I don't know how to handle it. – S. Deshmukh Oct 25 '18 at 09:18
  • 1
    You have to replace one " with two "" in your data. – Manoj Mohan Jan 09 '19 at 09:43
19
  1. Fields with embedded commas must be delimited with double-quote characters.

    fields:

    1. abc, xyz
    2. pqr

csv version:

"abc, xyz" , pqr
  1. Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.

    field:

    Welcome to "My World"
    

    csv version:

    "Welcome to ""My World"""
    
Liam
  • 27,717
  • 28
  • 128
  • 190
BrainCoder
  • 5,197
  • 5
  • 30
  • 33
7

If you have a comma in the data you need to put in a csv the https://www.rfc-editor.org/rfc/rfc4180 says to use quotations as:

"123,56","A dog, cat and a frog"
Community
  • 1
  • 1
Zesar
  • 566
  • 2
  • 14
3

Put Value in double quotes.

string ValueToEscape = "a,b";

"\"" + ValueToEscape + "\""

CSV Output = a,b

Liam
  • 27,717
  • 28
  • 128
  • 190
3
StringBuilder sb = new StringBuilder();          
        foreach (DataColumn col in dt.Columns)
        {
            if (col.ColumnName.Contains(","))
            {
                sb.Append(String.Format("\"{0}\",", col.ColumnName));
            }
            else
            {
                sb.Append(String.Format("{0},", col.ColumnName));
            }
        }
Gaurav Dubey
  • 358
  • 4
  • 7
2

Write Comma separated value between double quote without space to create csv with single column for comaa separated values.

Ex. I have two columns Code & Description With Values Code01 & Val1,Val2,Val3. To create csv with given data write below line in notepad and save it with csv extension.

Code,Description
Code01,"Val1,Val2,Val3"
Ravi Pipaliya
  • 219
  • 1
  • 9