6

我有一个CSV 格式的日志,我们为某个日志记录操作写出。但是,其中一个字段允许用户输入,我需要确保如果他们在字段中输入逗号,我们会将其解析出来并将其替换为Excel 将能够读取并在其中显示逗号的内容位置(因此 csv 阅读器不会认为它是列的结尾)。

目前我将逗号替换为,,但这在 Excel中显示为文字字符串。

是否有一种标准方法可以在不使用实际逗号字符的情况下在 CSV 文件中显示逗号?即使是仅适用于 excel 的解决方案也可以使用,因为我们的大多数客户将使用 Excel 来查看此文件。

4

4 回答 4

20

处理嵌入逗号的最佳方法是正确引用 CSV 文件:

  • 应引用包含逗号的列
  • 包含引号的带引号的列应该对引号进行转义

例子:

乔·史密斯,“小乔·史密斯”,“乔”“男人”“小史密斯”

我写了一个扩展方法来帮助解决这个问题:

static public string CsvQuote(this string text)
{
    if (text == null) return string.Empty;

    bool containsQuote = false;
    bool containsComma = false;
    int len = text.Length;

    for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++)
    {
        char ch = text[i];
        if (ch == '"')
        {
            containsQuote = true;
        }
        else if (ch == ',' || char.IsControl(ch))
        {
            containsComma = true;
        }
    }

    bool mustQuote = containsComma || containsQuote;

    if (containsQuote)
    {
        text = text.Replace("\"", "\"\"");
    }

    // Quote the cell and replace embedded quotes with double-quote or just return as is
    return mustQuote ? "\"" + text + "\"" : text;
}

用法:

logger.Write(myString.CsvQuote());

var csv = string.Join(",", listOfStrings.Select(CsvQuote))
于 2013-02-08T18:59:27.933 回答
1

Including your string inside of quotation marks will let you use commas.

"please sir,", can I, have some more?

于 2013-02-08T19:02:59.610 回答
0

CSV 也是“字符分隔值”,而不仅仅是逗号。

您可以使用任何字符作为分隔符,但tabor\t被广泛用于此,因为它通常不用于用户输入。

CSV 的 RFC 是RFC 4180

它建议使用数据字段和字段分隔符。这里是原文,请注意(5)中Microsoft Excel的特殊部分

5.  Each field may or may not be enclosed in double quotes (however
   some programs, such as Microsoft Excel, do not use double quotes
   at all).  If fields are not enclosed with double quotes, then
   double quotes may not appear inside the fields.  For example:

   "aaa","bbb","ccc" CRLF
   zzz,yyy,xxx

6.  Fields containing line breaks (CRLF), double quotes, and commas
   should be enclosed in double-quotes.  For example:

   "aaa","b CRLF
   bb","ccc" CRLF
   zzz,yyy,xxx

7.  If double-quotes are used to enclose fields, then a double-quote
   appearing inside a field must be escaped by preceding it with
   another double quote.  For example:

   "aaa","b""bb","ccc"

另请注意,Excel 开箱即用地识别 Tab

于 2013-02-08T18:59:49.753 回答
0

You can put a quotes around the entire field. Most CSV parsers will understand that the comma is part of the data and not the end of the field.

Or use a different separator. This will require you use the text import wizard in Excel instead of just being able to open the file directly. I typically use~ or |.

于 2013-02-08T19:01:09.723 回答