How should I escape commas and speech marks in CSV files so they work in Excel?

后端 未结 5 1135
说谎
说谎 2020-11-29 00:20

I\'m generating a CSV file (delimited by commas rather than tabs). My users will most likely open the CSV file in Excel by double clicking it. My data may contain commas and

相关标签:
5条回答
  • 2020-11-29 00:29

    Single quotes work fine too, even without escaping the double quotes, at least in Excel 2016:

    'text with spaces, and a comma','more text with spaces','spaces and "quoted text" and more spaces','nospaces','NOSPACES1234'
    

    Excel will put that in 5 columns (if you choose the single quote as "Text qualifier" in the "Text to columns" wizard)

    0 讨论(0)
  • 2020-11-29 00:33

    According to Yashu's instructions, I wrote the following function (it's PL/SQL code, but it should be easily adaptable to any other language).

    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;
    
    0 讨论(0)
  • 2020-11-29 00:44

    Even after double quotes, I had this problem for a few days.

    Replaced Pipe Delimiter with Comma, then things worked fine.

    0 讨论(0)
  • 2020-11-29 00:46

    We eventually found the answer to this.

    Excel will only respect the escaping of commas and speech marks if the column value is NOT preceded by a space. So generating the file without spaces like this...

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

    ... fixed the problem. Hope this helps someone!

    0 讨论(0)
  • 2020-11-29 00:48

    Below are the rules if you believe it's random. A utility function can be created on the basis of these rules.

    1. If the value contains a comma, newline or double quote, then the String value should be returned enclosed in double quotes.

    2. Any double quote characters in the value should be escaped with another double quote.

    3. If the value does not contain a comma, newline or double quote, then the String value should be returned unchanged.

    0 讨论(0)
提交回复
热议问题