Possible Duplicate:
Dealing with commas in a CSV file
We are exporting a bulk data into a csv file for one of our projects. So in this case we have to export values like a,b,c,d which all have to be remain in one column. But the comma will separate them to different columns.
Like if we export some values entered in textarea or editor which contains character like \r\n will be exported as separate rows in csv. How can i solve this problem??
// CSV rules: http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules
// From the rules:
// 1. if the data has quote, escape the quote in the data
// 2. if the data contains the delimiter (in our case ','), double-quote it
// 3. if the data contains the new-line, double-quote it.
if (data.Contains("\""))
{
data = data.Replace("\"", "\"\"");
}
if (data.Contains(","))
{
data = String.Format("\"{0}\"", data);
}
if (data.Contains(System.Environment.NewLine))
{
data = String.Format("\"{0}\"", data);
}
data could be individual items from db or a property value of a type.