Problems separating csv file using comma as a separator

0

I have an application that exports the result in a csv file. I'm using commas as a column separator, but the problem is that in one of the output parameters item.SN the content contains commas to separate the name then the file creates columns separating the data into several parts.

How could I fix this? I have tried several ways but I can not do it.

Thanks in advance.

Here is my code:

foreach (WS.ProjectMetaData proj in pr)
{ 
    string outCsvFile = string.Format(@"C:\ExportTasksForAllProjects\{0}.csv", proj.ProjectTitle + DateTime.Now.ToString("_yyyyMMdd HHmms"));
    String newLine = "";
    var stream = File.CreateText(outCsvFile);
    stream.WriteLine("ProjectName,Subject Name, UserID, Task StartDate");


     WS.UserData[] userDataId = client.GetUserData(DataSourceId);

        List<string> multiUserIDs = new List<string>();

            foreach (var userdata in userDataId)
            {
                multiUserIDs.Add(userdata.List[0].ToString());
            }

                string temp = "";   
                var AllProjectIds = proj.ProjectID;
                string[] projectIDs = new string[] { AllProjectIds }; // all projects       

                WS.TaskEntry[] resultGT3 = client.GetTasks3(projectIDs, multiUserIDs.ToArray());

                foreach (var item in resultGT3)
                {                                        
                    newLine = item.ProjectTitle + "," +
                                item.SN + "," +         //Aqui el SN contiene commas entonces se separa en diferentes columnas              
                                item.UserID + "," +
                                item.StartDate;                                        

                    stream.WriteLine(newLine);
                }
                stream.Close(); 
}
    
asked by A arancibia 08.03.2016 в 22:01
source

3 answers

1

When you have values that contain commas you must delimit it with quotes "

"Valor 1","Texto, que, contiene, comas","Valor 3"

(Important, if you are opening the file in Excel, there should be no space between the fields, ie "uno","dos" no "uno", "dos" )

Then it would be easier if you do this for all your values

newLine = string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"", 
    item.ProjectTitle,
    item.SN,
    item.UserID,
    item.StartDate);
    
answered by 08.03.2016 / 22:51
source
0

If we do not refer to the specifications of a csv

Comma-separated values

in the title " Basic rules " describes

  • Fields with embedded commas or double-quote characters must be quoted.
  

1997, Ford, E350, "Super, luxurious truck"

  • Each of the embedded double-quote characters must be represented by a pair of double-quote characters.
  

1997, Ford, E350, "Super," "luxurious" "truck"

You should follow the rules of the standard to generate your files.

    
answered by 08.03.2016 в 23:12
0

Since you have your code you can add quotes, to keep the specification :

foreach (var item in resultGT3)
        {                                        
          newLine = "\"" +item.ProjectTitle + "\",\"" +
          item.SN + "\",\"" +         //Aqui el SN contiene commas entonces se separa en diferentes columnas              
          item.UserID + "\",\"" +
          item.StartDate +"\"";                                          
          stream.WriteLine(newLine);
        }

or

foreach (var item in resultGT3)
        {    
         newLine = string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"", 
         item.ProjectTitle,
         item.SN,
         item.UserID,
         item.StartDate);
         stream.WriteLine(newLine);
        }

For example you would get:

"Valor 1","Otro, Valor, ok" , "valor 3" , Valor 4", "Valor 5"
    
answered by 08.03.2016 в 23:14