Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. public string GetCsv(string[] columns, List<object>[] data)
  2. {
  3. StringBuilder CsvData = new StringBuilder();
  4.  
  5. //add column headers
  6. string[] s = new string[Columns.Count];
  7. for (Int32 j = 0; j < Columns.Count; j++)
  8. {
  9. s[j] = columns[j];
  10. if (s[j].Contains(""")) //replace " with ""
  11. s[j].Replace(""", """");
  12. if (s[j].Contains(""") || s[j].Contains(" ")) //add "'s around any string with space or "
  13. s[j] = """ + s[j] + """;
  14. }
  15. CsvData.AppendLine(string.Join(",", s));
  16.  
  17. //add rows
  18. foreach (var row in data)
  19. {
  20. for (int j = 0; j < Columns.Count; j++)
  21. {
  22. s[j] = row[j] == null ? "" : row[j].ToString();
  23. if (s[j].Contains(""")) //replace " with ""
  24. s[j].Replace(""", """");
  25. if (s[j].Contains(""") || s[j].Contains(" ")) //add "'s around any string with space or "
  26. s[j] = """ + s[j] + """;
  27. }
  28. CsvData.AppendLine(string.Join(",", s));
  29. }
  30.  
  31. return CsvData.ToString();
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement