Navin kumar

Navin kumar

  • NA
  • 13
  • 8.9k

How to escape comma in csv file without using double quotes/" in C#

Sep 18 2020 12:59 AM
I am able to escape comma between strings with using double quotes such as strings are:"Laxchaman","Bharath","satrudhan" was brother of Ram.It is working fine in csv file But when i am copy the string from the csv cell to notepad it is showing """Laxchaman"",""Bharath"",""satrudhan"" was brother of Ram". That's why the csv file is not being upload in SQL. I want String must be write in csv file with comma as same as sentences in real form.
  1. using (DataTable dt = new DataTable())  
  2. {  
  3. dt.Load(reader);  
  4. Console.WriteLine(dt.Rows.Count); //check its filled  
  5. StringBuilder sb = new StringBuilder();  
  6. string sbb = "";  
  7. sb.AppendLine(string.Join(",", dt.Columns.Cast<datacolumn>().Select(c => c.ColumnName)));  
  8. sb.Remove(sb.Length - 1, 1);  
  9. foreach (DataRow row in dt.Rows)  
  10. {  
  11. for (int i = 0; i < dt.Columns.Count; i++)  
  12. {  
  13. if (row[i].ToString().Contains(","))  
  14. {  
  15. sb.Append(String.Format("\"{0}\",", row[i]));  
  16. }  
  17. else  
  18. {  
  19. sb.Append(row[i].ToString() + ",");  
  20. }  
  21. }  
  22. sb.AppendLine();  
  23. }  
  24. ArrayList ar = new ArrayList();  
  25. File.WriteAllText(@"D:\ISR\1" + ".csv", sb.ToString());  
  26. }  

Answers (2)