结果中未打印在字符串中找到(转义)的引号

Quotation found (escaped) in string is not being printed in result

我正在创建数据记录器。它需要以CSV格式记录数据和公式,以便稍后导入到Excel.

我几乎可以正常使用它,但是在导入到 Excel 时,我的内部(文字)引用之一丢失了。并且(可能因此)我的字符串被字符串中的以下逗号分成两个单元格。

我希望在单元格中看到原始公式:

=AVERAGE(INDIRECT(ADDRESS(1,COLUMN())&":"&ADDRESS(ROW()-1,COLUMN())))

代码:

String formula = "\"=AVERAGE(INDIRECT(ADDRESS(ROW()-3,COLUMN())&\":\"&ADDRESS(ROW()-1,COLUMN())))\"";
dataFile.println(formula);

结果Excel:

Cell 1: =AVERAGE(INDIRECT(ADDRESS(ROW()-3,COLUMN())&:"&ADDRESS(ROW()-1
Cell 2: COLUMN())))"

如何正确转义该引语?我一直以为它只是“\”“。​​这能解决我的逗号分隔问题吗?

CSV 规范要求使用额外的双引号对双引号进行转义。字符串最终应该是

"=AVERAGE(INDIRECT(ADDRESS(ROW()-3,COLUMN())&"":""&ADDRESS(ROW()-1,COLUMN())))"

您需要添加适当的 c# 转义符,但这应该可以解决问题。

string MyString = "\"=AVERAGE(INDIRECT(ADDRESS(ROW()-3,COLUMN())&\"\":\"\"&ADDRESS(ROW()-1,COLUMN())))\""

https://www.rfc-editor.org/rfc/rfc4180

  1. 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"