CSV for Excel, Including Both Leading Zeros and Commas

后端 未结 12 915
再見小時候
再見小時候 2020-12-02 18:45

I want to generate a CSV file for user to use Excel to open it.

If I want to escape the comma in values, I can write it as \"640,480\".

If I want to keep the

12条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 19:29

    Put a prefix String on your data:

     "N001,002","N002,003" 
    

    ( As long as that prefix is not an E )

    That notation ( In OpenOffice at least) above parses as a total of 2 columns with the N001,002 bytes correctly stored.

    CSV Specification says that , is permitted inside quote strings.

    Also, A warning from experience: make sure you do this with phone numbers too. Excel will otherwise interpret phone numbers as a floating point number and save them in scientific notation :/ , and 1.800E10 is not a really good phone number.

    In OpenOffice, this RawCSV chunk also decodes as expected:

      "=""001,002""","=""002,004"""
    

    ie:

       $rawdata = '001,002'; 
       $equation = "=\"$rawdata\"";
       $escaped = str_replace('"','""',$equation); 
       $csv_chunk = "\"$escaped\"" ; 
    

提交回复
热议问题