My question is about CSV file in C#. If a field contains a comma how can this be exported into a CSV file as a one field? See Field 2 in the sample below:
Field1|Field2|Field3
10.20|Doe,John|Box No 1
I have this code:
//Get indices
indices = DenormalizeIndices(id);
foreach (DataRow row in indices.Tables[0].Rows)
{
line = null;
//We're only going to use selected fields
for (i = 0; i < 3; i++)
{
if (line != null)
line += DELIMITER;
//Figure out the mapped field name we have defined
if (i >= INDICES_TO_INCLUDE.Length)
line += ""; //Nothing to populate
else
if (String.IsNullOrEmpty(INDICES_TO_INCLUDE[i]))
line += "";
else
line += "=\"" + row[INDICES_TO_INCLUDE[i]].ToString() + "\"";
But with this code above the CSV file result will be as below, it will separate the index value of Field2:
Field1 | Field2 | Field3 | Field4
10.20 | Doe | John | Box No 1
What else should I add to the code so the result will be as below on Field2?
Field2
Doe, John
|will not give you aCSVfile.=signs before each field