Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string GetCsv(string[] columns, List<object>[] data)
- {
- StringBuilder CsvData = new StringBuilder();
- //add column headers
- string[] s = new string[Columns.Count];
- for (Int32 j = 0; j < Columns.Count; j++)
- {
- s[j] = columns[j];
- if (s[j].Contains(""")) //replace " with ""
- s[j].Replace(""", """");
- if (s[j].Contains(""") || s[j].Contains(" ")) //add "'s around any string with space or "
- s[j] = """ + s[j] + """;
- }
- CsvData.AppendLine(string.Join(",", s));
- //add rows
- foreach (var row in data)
- {
- for (int j = 0; j < Columns.Count; j++)
- {
- s[j] = row[j] == null ? "" : row[j].ToString();
- if (s[j].Contains(""")) //replace " with ""
- s[j].Replace(""", """");
- if (s[j].Contains(""") || s[j].Contains(" ")) //add "'s around any string with space or "
- s[j] = """ + s[j] + """;
- }
- CsvData.AppendLine(string.Join(",", s));
- }
- return CsvData.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement