Is it possible to force Excel recognize UTF-8 CSV files automatically?

后端 未结 27 1651
醉梦人生
醉梦人生 2020-11-21 22:27

I\'m developing a part of an application that\'s responsible for exporting some data into CSV files. The application always uses UTF-8 because of its multilingual nature at

相关标签:
27条回答
  • 2020-11-21 22:45

    This is not accurately addressing the question but since i stumbled across this and the above solutions didn't work for me or had requirements i couldn't meet, here is another way to add the BOM when you have access to vim:

    vim -e -s +"set bomb|set encoding=utf-8|wq" filename.csv
    
    0 讨论(0)
  • 2020-11-21 22:48

    You can convert .csv file to UTF-8 with BOM via Notepad++:

    1. Open the file in Notepad++.
    2. Go to menu EncodingConvert to UTF-8.
    3. Go to menu FileSave.
    4. Close Notepad++.
    5. Open the file in Excel .

    Worked in Microsoft Excel 2013 (15.0.5093.1000) MSO (15.0.5101.1000) 64-bit from Microsoft Office Professional Plus 2013 on Windows 8.1 with locale for non-Unicode programs set to "German (Germany)".

    0 讨论(0)
  • 2020-11-21 22:48

    This is an old question but I've just encountered had a similar problem and the solution may help others:

    Had the same issue where writing out CSV text data to a file, then opening the resulting .csv in Excel shifts all the text into a single column. After having a read of the above answers I tried the following, which seems to sort the problem out.

    Apply an encoding of UTF-8 when you create your StreamWriter. That's it.

    Example:

    using (StreamWriter output = new StreamWriter(outputFileName, false, Encoding.UTF8, 2 << 22)) {
       /* ... do stuff .... */
       output.Close();
    }
    
    0 讨论(0)
  • 2020-11-21 22:49

    The bug with ignored BOM seems to be fixed for Excel 2013. I had same problem with Cyrillic letters, but adding BOM character \uFEFF did help.

    0 讨论(0)
  • 2020-11-21 22:49

    Working solution for office 365

    • save in UTF-16 (no LE, BE)
    • use separator \t

    Code in PHP

    $header = ['číslo', 'vytvořeno', 'ěščřžýáíé'];
    $fileName = 'excel365.csv';
    $fp = fopen($fileName, 'w');
    fputcsv($fp, $header, "\t");
    fclose($fp);
    
    $handle = fopen($fileName, "r");
    $contents = fread($handle, filesize($fileName));
    $contents = iconv('UTF-8', 'UTF-16', $contents);
    fclose($handle);
    
    $handle = fopen($fileName, "w");
    fwrite($handle, $contents);
    fclose($handle);
    
    0 讨论(0)
  • 2020-11-21 22:51

    Alex is correct, but as you have to export to csv, you can give the users this advice when opening the csv files:

    1. Save the exported file as a csv
    2. Open Excel
    3. Import the data using Data-->Import External Data --> Import Data
    4. Select the file type of "csv" and browse to your file
    5. In the import wizard change the File_Origin to "65001 UTF" (or choose correct language character identifier)
    6. Change the Delimiter to comma
    7. Select where to import to and Finish

    This way the special characters should show correctly.

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