我正在生成一个CSV文件(用逗号而不是制表符分隔)。我的用户很可能会通过双击在Excel中打开CSV文件。我的数据可能包含逗号和语音标记,因此我将按如下方式进行转义。
Reference, Title, Description
1, "My little title", "My description, which may contain ""speech marks"" and commas."
2, "My other little title", "My other description, which may also contain ""speech marks"" and commas."
据我所知,这一直都是这样做的。这是我的困惑:当我在Excel2010中打开这个文件时,我的转义没有得到尊重。讲话标记出现在工作表上,逗号会导致新的列。
发布于 2012-09-18 16:50:37
我们最终找到了这个问题的答案。
如果列值前面没有空格,Excel将只考虑逗号和语音标记的转义。所以生成没有像这样的空格的文件...
Reference,Title,Description
1,"My little title","My description, which may contain ""speech marks"" and commas."
2,"My other little title","My other description, which may also contain ""speech marks"" and commas."
..。已修复此问题。希望这对某些人有帮助!
发布于 2015-05-26 22:17:16
根据Yashu的指示,我编写了以下函数(这是PL/SQL代码,但它应该可以很容易地适应任何其他语言)。
FUNCTION field(str IN VARCHAR2) RETURN VARCHAR2 IS
C_NEWLINE CONSTANT CHAR(1) := '
'; -- newline is intentional
v_aux VARCHAR2(32000);
v_has_double_quotes BOOLEAN;
v_has_comma BOOLEAN;
v_has_newline BOOLEAN;
BEGIN
v_has_double_quotes := instr(str, '"') > 0;
v_has_comma := instr(str,',') > 0;
v_has_newline := instr(str, C_NEWLINE) > 0;
IF v_has_double_quotes OR v_has_comma OR v_has_newline THEN
IF v_has_double_quotes THEN
v_aux := replace(str,'"','""');
ELSE
v_aux := str;
END IF;
return '"'||v_aux||'"';
ELSE
return str;
END IF;
END;
发布于 2018-06-12 18:19:09
单引号也可以,即使没有转义双引号,至少在Excel 2016中是这样:
'text with spaces, and a comma','more text with spaces','spaces and "quoted text" and more spaces','nospaces','NOSPACES1234'
Excel会将其放在5列中(如果您在“文本到列”向导中选择了单引号作为“文本限定符”)
https://stackoverflow.com/questions/12473480
复制相似问题