Microsoft Excel mangles Diacritics in .csv files?

后端 未结 22 1704
粉色の甜心
粉色の甜心 2020-11-22 05:02

I am programmatically exporting data (using PHP 5.2) into a .csv test file.
Example data: Numéro 1 (note the accented e). The data is utf-8 (

相关标签:
22条回答
  • 2020-11-22 05:23

    The answer for all combinations of Excel versions (2003 + 2007) and file types

    Most other answers here concern their Excel version only and will not necessarily help you, because their answer just might not be true for your version of Excel.

    For example, adding the BOM character introduces problems with automatic column separator recognition, but not with every Excel version.

    There are 3 variables that determines if it works in most Excel versions:

    • Encoding
    • BOM character presence
    • Cell separator

    Somebody stoic at SAP tried every combination and reported the outcome. End result? Use UTF16le with BOM and tab character as separator to have it work in most Excel versions.

    You don't believe me? I wouldn't either, but read here and weep: http://wiki.sdn.sap.com/wiki/display/ABAP/CSV+tests+of+encoding+and+column+separator

    0 讨论(0)
  • 2020-11-22 05:24

    I've found a way to solve the problem. This is a nasty hack but it works: open the doc with Open Office, then save it into any excel format; the resulting .xls or .xlsx will display the accentuated characters.

    0 讨论(0)
  • 2020-11-22 05:26

    select UTF-8 enconding when importing. if you use Office 2007 this is where you chose it : right after you open the file.

    0 讨论(0)
  • 2020-11-22 05:27

    I can only get CSV to parse properly in Excel 2007 as tab-separated little-endian UTF-16 starting with the proper byte order mark.

    0 讨论(0)
  • 2020-11-22 05:29

    I've also noticed that the question was "answered" some time ago but I don't understand the stories that say you can't open a utf8-encoded csv file successfully in Excel without using the text wizard.

    My reproducible experience: Type Old MacDonald had a farm,ÈÌÉÍØ into Notepad, hit Enter, then Save As (using the UTF-8 option).

    Using Python to show what's actually in there:

    >>> open('oldmac.csv', 'rb').read()
    '\xef\xbb\xbfOld MacDonald had a farm,\xc3\x88\xc3\x8c\xc3\x89\xc3\x8d\xc3\x98\r\n'
    >>> ^Z
    

    Good. Notepad has put a BOM at the front.

    Now go into Windows Explorer, double click on the file name, or right click and use "Open with ...", and up pops Excel (2003) with display as expected.

    0 讨论(0)
  • 2020-11-22 05:31

    You can save an html file with the extension 'xls' and accents will work (pre 2007 at least).

    Example: save this (using Save As utf8 in Notepad) as test.xls:

    <html>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
    <table>
    <tr>
      <th>id</th>
      <th>name</th>
    </tr>
    <tr>
     <td>4</td>
     <td>Hélène</td>
    </tr>
    </table>
    </html>
    
    0 讨论(0)
提交回复
热议问题