I have an application that exports the result in a CSV file. I am using commas as column separator, but the problem is that in one of the output parameters item.SN
the content also contains commas to separate the name; then the file creates columns by separating the data into various parts.
How could I fix this? I have tried several ways but I can't 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();
}
When you have values that contain commas you must delimit it with quotes
"
(Important, if you are opening the file in Excel, there must not be any spaces between the fields, ie
"uno","dos"
no"uno", "dos"
)So it would be easier if you do this for all your values
If we do not refer to the specifications of a
csv
Comma-separated values
in the title " Basic rules " describes
You should follow the rules of the standard to generate your files.
Since you have your code you can add quotes, to keep the specification :
either
For example you would get:
namespace WsConsultaExamen { public class ConsultModel { public string C40FUM { get; set; } public string C40NID { get; set; } public string C40TID { get; set; } public string C40DIG { get; set; } public string C40NIP { get; set; } public string C40FCC { get; set; } public string C40NOM { get; set; } public string C40TNO { get; set; } public string C40GRF { get; set; } public string C40FIN { get; set; } public string C40LNE { get; set; } public string C40FPI { get; set; } public string C40TRF { get; set; } public string C40GER { get; set; } public string C40AUC { get; set; } public string C40CLP { get; set; } public string C40SUC { get; set; }
}