wpf 将 DataTable 导出为 CSV 时的逗号问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14937984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comma issue when exporting DataTable to CSV
提问by Dot NET
I've adopted some code which converts a DataTable into a CSV file. It seems to work well, except for when commas are used in the actual data. Is there a way to display the comma in that case? This is what I've done:
我采用了一些将 DataTable 转换为 CSV 文件的代码。它似乎工作得很好,除非在实际数据中使用逗号。在这种情况下有没有办法显示逗号?这就是我所做的:
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = dtResults.Columns
.Cast<DataColumn>()
.Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dtResults.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(saveFileDialog.FileName, sb.ToString());
回答by Paolo Tedesco
If a field contains a comma it should be put in double (or single) quotes.
如果字段包含逗号,则应将其放在双(或单)引号中。
You could use the FileHelpers libraryto create your CSV file, it should take care of escaping properly.
您可以使用FileHelpers 库来创建您的 CSV 文件,它应该负责正确转义。
In case you don't want to use the library, the essential bit is this: if you have a comma, you must put your field within quotes, e.g.
如果您不想使用该库,那么重要的一点是:如果您有逗号,则必须将您的字段放在引号内,例如
ID, NAME
1, "Doe, John"
2, 'Doe, Jane'
If your field includes quotes, you should escape them with an additional quote:
如果您的字段包含引号,您应该使用额外的引号将它们转义:
3, "Anakin ""Darth Vader"", Skywalker"
4, 'O''Connor, Sinead'
A possible solution:
一个可能的解决方案:
static string CsvEscape(this string value) {
if (value.Contains(",")) {
return "\"" + value.Replace("\"", "\"\"") + "\"";
}
return value;
}
And then in your code:
然后在你的代码中:
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString().CsvEscape());
P.S: according to Wikipediathere is no real csv specification, but RFC 4180describes a format that is often used.
回答by Smaug
I published the below modified code which will help you.
我发布了以下修改后的代码,这将对您有所帮助。
StringBuilder sb = new StringBuilder();
string[] val = { "ramesh","suresh, Ganesh" };
IEnumerable<string> columnNames = val;
IList<string> objFinal = new List<string>();
foreach (string v in columnNames)
{
string temp = v;
if (temp.Contains(","))
temp = "\"" + v + "\"";
objFinal.Add(temp);
}
sb.AppendLine(string.Join(",", objFinal.AsEnumerable<string>()));
}
The CSV supports the comma in data between the double codes.
CSV 支持双码之间数据中的逗号。
You can append the double codes as well as comma in a single shot with help of the below single line of code
您可以在以下单行代码的帮助下一次性附加双代码和逗号
IEnumerable<string> columnNames = dtResults.Columns
.Cast<DataColumn>()
.Select(column => column.ColumnName);
var res = columnNames.Aggregate((current, next) => "\"" + current + "\"" + ", " + "\"" + next + "\"");
File.WriteAllText(saveFileDialog.FileName, res.ToString());

