可能重复:
在CSV文件中处理逗号
我们正在将批量数据导出到我们的一个项目的csv文件中.所以在这种情况下,我们必须输出像a,b,c,d这样的值,这些值都必须保留在一列中.但是逗号会将它们分成不同的列.
就像我们导出一些在textarea中输入的值或包含\ r \n之类字符的编辑器将导出为csv中的单独行.我怎么解决这个问题??
Vij*_*iri 60
// 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("\"", "\"\"");
}
if (data.Contains(","))
{
data = String.Format("\"{0}\"", data);
}
if (data.Contains(System.Environment.NewLine))
{
data = String.Format("\"{0}\"", data);
}
Run Code Online (Sandbox Code Playgroud)
数据可以是来自db的单个项目,也可以是类型的属性值.
| 归档时间: |
|
| 查看次数: |
32157 次 |
| 最近记录: |