-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
30 lines (26 loc) · 922 Bytes
/
Copy pathExtensions.cs
File metadata and controls
30 lines (26 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Data;
// Copyright © 2016-2019 ASM-SW
//asmeyers@outlook.com https://github.com/asm-sw
using System.Text;
public static class Extensions
{
public static string ToCSV(this DataTable table)
{
// reference: adapted from http://stackoverflow.com/questions/888181/convert-datatable-to-csv-stream
var result = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
result.AppendFormat("\"{0}\"",table.Columns[i].ColumnName);
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
result.AppendFormat("\"{0}\"", row[i].ToString());
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
}
return result.ToString();
}
}