可能重复:
Dealing with commas in a CSV file
我们正在为我们的一个项目将批量数据导出到csv文件。所以在这种情况下,我们必须导出a,b,c,d这样的值,它们都必须保留在一列中。但是逗号会将它们分隔成不同的列。
例如,如果我们导出一些在文本区域或编辑器中输入的值,其中包含字符,例如\r\n将作为csv中的单独行导出。我该如何解决这个问题??
发布于 2011-04-21 14:32:14
处理这种情况最常见的方法是使用"" around字段,但根据谁在使用您的文件,可以采用多种方式进行处理。您可以分隔逗号,可以将逗号更改为特殊值,也可以使用不同的分隔符,但most命令
发布于 2011-04-21 14:43:21
// CSV rules: http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules
// From the rules:
// 1. if the data has quote, escape the quote in the data
// 2. if the data contains the delimiter (in our case ','), double-quote it
// 3. if the data contains the new-line, double-quote it.
if (data.Contains("\""))
{
data = data.Replace("\"", "\"\"");
data = String.Format("\"{0}\"", data);
}
else if (data.Contains(",") || data.Contains(System.Environment.NewLine))
{
data = String.Format("\"{0}\"", data);
}
数据可以是数据库中的单个项,也可以是某一类型的属性值。
https://stackoverflow.com/questions/5740359
复制相似问题