205

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 (no prepended BOM).

When I open this file in MS Excel is displays as Numéro 1.

I am able to open this in a text editor (UltraEdit) which displays it correctly. UE reports the character is decimal 233.

How can I export text data in a .csv file so that MS Excel will correctly render it, preferably without forcing the use of the import wizard, or non-default wizard settings?

8
  • I would be very interested in hearing more about your BOM solution, as I believe I've tried "EF BB BF" which did not work for me. Oct 4, 2008 at 18:28
  • 3
    The working solution chosen was to: * include a BOM ; utf-8 * use this header: 'Content-type: text/plain; charset=utf-8' This "worked" in excel 2003 and excel 2007 -- where worked = opened without an import wizard and rendered diacritics correctly. I did not verify that the BOM was required.
    – Freddo411
    Oct 13, 2008 at 20:56
  • 2
    The BOM is required, I just tested this now. Without it the special chars don't render ok. Mar 22, 2010 at 12:12
  • 2
    would love it if someone could say more about how to add a BOM (byte-order marker). If I just do something like Response.Write(EF BB BF") those characters just show up at the beginning of the file.
    – sydneyos
    Sep 16, 2011 at 0:06
  • sydneyos: As Fergal says below; Prepend \uFEFF to your string.
    – noocyte
    Oct 26, 2011 at 10:31

22 Answers 22

266

A correctly formatted UTF8 file can have a Byte Order Mark as its first three octets. These are the hex values 0xEF, 0xBB, 0xBF. These octets serve to mark the file as UTF8 (since they are not relevant as "byte order" information).1 If this BOM does not exist, the consumer/reader is left to infer the encoding type of the text. Readers that are not UTF8 capable will read the bytes as some other encoding such as Windows-1252 and display the characters  at the start of the file.

There is a known bug where Excel, upon opening UTF8 CSV files via file association, assumes that they are in a single-byte encoding, disregarding the presence of the UTF8 BOM. This can not be fixed by any system default codepage or language setting. The BOM will not clue in Excel - it just won't work. (A minority report claims that the BOM sometimes triggers the "Import Text" wizard.) This bug appears to exist in Excel 2003 and earlier. Most reports (amidst the answers here) say that this is fixed in Excel 2007 and newer.

Note that you can always* correctly open UTF8 CSV files in Excel using the "Import Text" wizard, which allows you to specify the encoding of the file you're opening. Of course this is much less convenient.

Readers of this answer are most likely in a situation where they don't particularly support Excel < 2007, but are sending raw UTF8 text to Excel, which is misinterpreting it and sprinkling your text with à and other similar Windows-1252 characters. Adding the UTF8 BOM is probably your best and quickest fix.

If you are stuck with users on older Excels, and Excel is the only consumer of your CSVs, you can work around this by exporting UTF16 instead of UTF8. Excel 2000 and 2003 will double-click-open these correctly. (Some other text editors can have issues with UTF16, so you may have to weigh your options carefully.)


* Except when you can't, (at least) Excel 2011 for Mac's Import Wizard does not actually always work with all encodings, regardless of what you tell it. </anecdotal-evidence> :)

25
  • 15
    Took me forever to find where to specify the encoding. Save Dialog > Tools Button > Web Options > Encoding Tab. They sure are good at hiding such important things.
    – Triynko
    May 17, 2010 at 20:31
  • 7
    Wrong: adding a BOM to an UTF-8 file loads that file correctly without requiring the import wizard in Excel 2007. Jan 24, 2011 at 10:28
  • 3
    We found the same thing as Victor says today (using Excel 2010, it's all we had available). Adding a UTF-8 BOM/Signature (EF BB BF) seemed to fix the double-clicking using the system default encoding, and correctly uses UTF8 :) Mar 2, 2011 at 17:56
  • 22
    In general, a UTF-8-encoded file should not have a Byte Order Mark prepended. UTF-8 does not have variable byte order, and putting it there sabotages UTF-8's ASCII compatibility. There are some specific file formats that either allow or encourage a UTF-8 faux-BOM, but otherwise it should be avoided. CSV is entirely encoding-ignorant so it's anyone's guess as to whether a given tool will interpret the byte sequence 0xEF 0xBB 0xBF as an indicator of UTF-8; an invisible control character in the first cell; the characters  in the first cell; or something else entirely.
    – bobince
    Apr 14, 2012 at 10:38
  • 4
    @Ian: Nobody knows for sure it's UTF-8 with a BOM either - 0xEF 0xBB 0xBF is a valid sequence in most legacy encodings too (hence it often being misinterpreted as ISO-8859-1 or cp1252 and displayed as ). It only helps guessing algorithms, and for file formats that specifically make allowances for it (eg XML). The downside to including a faux-BOM in UTF-8 files is you break their ASCII-compatibility (a major selling point for UTF-8) Many encoding-ignorant text tools will break faced with an unexpected leading faux-BOM.
    – bobince
    Sep 6, 2012 at 21:40
43

Prepending a BOM (\uFEFF) worked for me (Excel 2007), in that Excel recognised the file as UTF-8. Otherwise, saving it and using the import wizard works, but is less ideal.

7
  • 1
    It still opens the text import wizard, so the difference is that you can simply double click, so still not ideal but the only known solution anyway.
    – haridsv
    Apr 30, 2010 at 22:57
  • For me, no import wizard appears with Excel 2007. Jan 24, 2011 at 10:28
  • No import wizard for me either - it works as expected if a UTF8 BOM/Signature (EF BB BF) is present. Mar 2, 2011 at 17:57
  • Also, \ufeff is a UTF-16 (BE) BOM not a UTF-8 BOM Jan 7, 2015 at 13:51
  • 4
    No, @AlastairMcCormack, it's either, depending on how it's encoded. "\ufeff" encoded as UTF-8 is exactly EF BB BF. (Encoded as UTF-16 it will be just two bytes.)
    – Dave Burt
    Jan 13, 2015 at 0:38
34

Below is the PHP code I use in my project when sending Microsoft Excel to user:

  /**
   * Export an array as downladable Excel CSV
   * @param array   $header
   * @param array   $data
   * @param string  $filename
   */
  function toCSV($header, $data, $filename) {
    $sep  = "\t";
    $eol  = "\n";
    $csv  =  count($header) ? '"'. implode('"'.$sep.'"', $header).'"'.$eol : '';
    foreach($data as $line) {
      $csv .= '"'. implode('"'.$sep.'"', $line).'"'.$eol;
    }
    $encoded_csv = mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');
    header('Content-Description: File Transfer');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: '. strlen($encoded_csv));
    echo chr(255) . chr(254) . $encoded_csv;
    exit;
  }

UPDATED: Filename improvement and BUG fix correct length calculation. Thanks to TRiG and @ivanhoe011

5
  • 1
    I tried several other suggestions on this page, but this worked for me in Excel 2007. The most important changes were to use tabs instead of commas (even though it's a .csv file) and the line above that echos the two characters followed by the call to mb_convert_encoding(). I also had to recompile PHP with --enable-mbstring to get support for mb_convert_encoding(). Thanks!
    – Russell G
    Jan 10, 2012 at 23:49
  • 1
    This worked well for me too, thanks. However, in Safari I get an error in my console 'Resource interpreted as document but transferred as...' I guess it's a WebKit quirk, judging stackoverflow.com/questions/3899426/…, but perhaps it's not and/or someone has found a solution. Furthermore, in your example I would suggest a change: 'Content-Disposition: attachment; filename="'.$filename.'.csv"' because Firefox wants the double quotes, or else it will cut off your filename after a space.
    – kasimir
    Mar 20, 2012 at 13:07
  • Why are you outputting CSV (text/csv) but calling it Excel (application/vnd.ms-excel)?
    – TRiG
    Mar 12, 2013 at 14:32
  • 2
    This works great! I can confirm it's working on Mac as well (in Office 2011).
    – Jonathan
    Jul 1, 2013 at 0:29
  • 1
    Shouldn't this be header('Content-Length: '. mb_strlen($encoded_csv, 'UTF-16LE')); ? Sep 30, 2015 at 15:20
15

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

4
  • Why not just add sep=, or whatever you want to use? If you're already adding the BOM I assume you're not averse to adding stuff to the file.
    – Casey
    Mar 5, 2015 at 17:37
  • Well, actually, to answer my own question, you wouldn't add the field separator declaration because it causes this trick to stop working. So basically it's garbled encoding or your file not being interpreted properly as a CSV if your users have the wrong region settings.
    – Casey
    Mar 5, 2015 at 17:49
  • 2
    utf-16le + BOM(0xFF 0xFE) + tab is the best
    – zhaozhi
    May 29, 2015 at 13:44
  • Ahh this recommendation explains why Pandas wouldn't open an "Excel" file I was supplied with until I manually worked out it was utf-16-le encoded and tab delimited. Feb 27, 2022 at 22:39
10

Echo UTF-8 BOM before outputing CSV data. This fixes all character issues in Windows but doesnt work for Mac.

echo "\xEF\xBB\xBF";

It works for me because I need to generate a file which will be used on Windows PCs only.

3
  • Not true for every type of column separator nor every Excel version. Read my answer below (below for now). Jun 13, 2014 at 9:38
  • Where should I put this? :-)
    – Denny
    Oct 24, 2022 at 9:54
  • It works on current Macs, too
    – Wieczo
    Jan 17 at 7:45
10

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

1
  • 1
    This is useful. I have modified the question to ask how to do this without resorting to the wizard
    – Freddo411
    Sep 30, 2008 at 21:45
7

UTF-8 doesn't work for me in office 2007 without any service pack, with or without BOM (U+ffef or 0xEF,0xBB,0xBF , neither works) installing sp3 makes UTF-8 work when 0xEF,0xBB,0xBF BOM is prepended.

UTF-16 works when encoding in python using "utf-16-le" with a 0xff 0xef BOM prepended, and using tab as seperator. I had to manually write out the BOM, and then use "utf-16-le" rather then "utf-16", otherwise each encode() prepended the BOM to every row written out which appeared as garbage on the first column of the second line and after.

can't tell whether UTF-16 would work without any sp installed, since I can't go back now. sigh

This is on windows, dunno about office for MAC.

for both working cases, the import works when launching a download directly from the browser and the text import wizard doesn't intervence, it works like you would expect.

2
  • Works on Excel 2011 for Mac too.
    – Adam
    May 25, 2013 at 1:22
  • thank you for your post, use utf-16le is ok even when you didn't install office 2007 sp3, but the BOM should be 0xFF 0xFE
    – zhaozhi
    May 29, 2015 at 13:43
4

As Fregal said \uFEFF is the way to go.

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<%
Response.Clear();
Response.ContentType = "text/csv";
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment; filename=excelTest.csv");
Response.Write("\uFEFF");
// csv text here
%>
1
  • 1
    Just watch and see how your tab separator is ignored in Excel 2007 when you use BOM. You have to come up with something more. Aug 22, 2013 at 8:31
3

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.

4
  • @Cocowalla: Well, I just tried this (again; I did test it before posting) and it worked with Excel 2007 (which is what I'm using now). Did you do open('oldmac.csv', 'rb').read() to verify your input? Dec 19, 2010 at 21:47
  • I didn't try with Excel 2007 (I know Excel 2007 reads UTF-8 files with a BOM just fine), I tried with Excel 2003
    – Cocowalla
    Dec 22, 2010 at 10:41
  • @Cocowalla: Well it worked for me with Excel 2003 when I had it. Are you sure you have the latest service pack for Excel 2003? Did you verify you input as I suggested? Dec 22, 2010 at 23:57
  • I did verify that notepad had stuck a BOM at the start of the file, but I'm on Excel 2003 SP2 (SP3 is available) - so I guess this only works in SP3
    – Cocowalla
    Dec 23, 2010 at 8:20
3

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>
2
  • interesting option. It opens the text right but for some reason all the page is completely white. Without the classic spreadsheet lines delimiting rows and columns (office for mac) Nov 24, 2011 at 12:18
  • Yup, same thing in Office 2007 on Windows. It's always surprised me that it worked at all, to be honest. (Note, if you add border="1" to the table, you do get lines, but just around the 4 cells :)
    – Benjol
    Nov 24, 2011 at 12:25
1

This is just of a question of character encodings. It looks like you're exporting your data as UTF-8: é in UTF-8 is the two-byte sequence 0xC3 0xA9, which when interpreted in Windows-1252 is é. When you import your data into Excel, make sure to tell it that the character encoding you're using is UTF-8.

5
  • I've confirmed that the data is UTF-8. What do I put into the file to let excel know that my data is utf-8 (BOM?)
    – Freddo411
    Sep 30, 2008 at 21:17
  • I think that you need to change the file encoding, excel uses the system default codepage to handle csv files
    – albertein
    Sep 30, 2008 at 21:19
  • I'm not entirely sure, since I don't have Excel installed on the machine I'm currently using, but with OpenOffice, there's a dropdown box for character encoding when you import a CSV file. From there, choose Unicode (UTF-8). Sep 30, 2008 at 21:21
  • Excel doesn't have the dropdown AFAIK
    – albertein
    Sep 30, 2008 at 21:23
  • check out this. works for me too. Feb 4, 2021 at 10:36
1

The CSV format is implemented as ASCII, not unicode, in Excel, thus mangling the diacritics. We experienced the same issue which is how I tracked down that the official CSV standard was defined as being ASCII-based in Excel.

3
  • Actually, CSV is not bound to a specific encoding. It's Excel that's assuming ASCII. en.wikipedia.org/wiki/Comma-separated_values
    – spoulson
    Jul 1, 2009 at 18:28
  • That's what I said. "implemented as ASCII in Excel", "CSV defined as ASCII-based in Excel". Not sure what point you're making as you appear to be agreeing with me.
    – Jeff Yates
    Jul 1, 2009 at 18:42
  • 2
    Actually you say "The CSV format is implemented as ASCI", I think that is where the confusion stems from.
    – RichardOD
    Oct 13, 2009 at 13:35
1

Excel 2007 properly reads UTF-8 with BOM (EF BB BF) encoded csv.

Excel 2003 (and maybe earlier) reads UTF-16LE with BOM (FF FE), but with TABs instead of commas or semicolons.

1

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.

1

Writing a BOM to the output CSV file actually did work for me in Django:

def handlePersoonListExport(request):
    # Retrieve a query_set
    ...

    template = loader.get_template("export.csv")
    context = Context({
        'data': query_set,
    })

    response = HttpResponse()
    response['Content-Disposition'] = 'attachment; filename=export.csv'
    response['Content-Type'] = 'text/csv; charset=utf-8'
    response.write("\xEF\xBB\xBF")
    response.write(template.render(context))

    return response

For more info http://crashcoursing.blogspot.com/2011/05/exporting-csv-with-special-characters.html Thanks guys!

2
1

Another solution I found was just to encode the result as Windows Code Page 1252 (Windows-1252 or CP1252). This would be done, for example by setting Content-Type appropriately to something like text/csv; charset=Windows-1252 and setting the character encoding of the response stream similarly.

2
  • Thanks for this one. Works on excel windows and mac. I'm using it. Nov 24, 2011 at 13:44
  • This would only work if your non-ascii character range falls entirely within Windows-1252. So for example, no korean/chinese/japanese, no cyrillic, etc. But I guess you'll slide by with this for most western european languages. Jun 7, 2012 at 20:02
1

Note that including the UTF-8 BOM is not necessarily a good idea - Mac versions of Excel ignore it and will actually display the BOM as ASCII… three nasty characters at the start of the first field in your spreadsheet…

1
  • I know this comment is 6 years later, but FWIW: Using JavaScript to download a file like '\uFEFF' + myCsvString works as expected on Mac Excel 15.19.1 (2016).
    – bobjones
    Apr 18, 2018 at 17:07
0

Check the encoding in which you are generating the file, to make excel display the file correctly you must use the system default codepage.

Wich language are you using? if it's .Net you only need to use Encoding.Default while generating the file.

2
  • The export data is utf-8. I am writing the export file with php 5
    – Freddo411
    Sep 30, 2008 at 21:18
  • Transcode the data to Windows-1252 codepage, i'm not sure how to acomplish it with php
    – albertein
    Sep 30, 2008 at 21:22
0

If you have legacy code in vb.net like I have, the following code worked for me:

    Response.Clear()
    Response.ClearHeaders()
    Response.ContentType = "text/csv"
    Response.Expires = 0
    Response.AddHeader("Content-Disposition", "attachment; filename=export.csv;")
    Using sw As StreamWriter = New StreamWriter(Context.Response.OutputStream, System.Text.Encoding.Unicode)
        sw.Write(csv)
        sw.Close()
    End Using
    Response.End()
0

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.

1
  • 1
    The OP says he's programmatically exporting, so he's not looking for a solution that needs manual intervention. Aug 22, 2013 at 8:29
0

With Ruby 1.8.7 I encode every field to UTF-16 and discard BOM (maybe).

The following code is extracted from active_scaffold_export:

<%                                                                                                                                                                                                                                                                                                                           
      require 'fastercsv'                                                                                                                                                                                                                                                                                                        
      fcsv_options = {                                                                                                                                                                                                                                                                                                           
        :row_sep => "\n",                                                                                                                                                                                                                                                                                                        
        :col_sep => params[:delimiter],                                                                                                                                                                                                                                                                                          
        :force_quotes => @export_config.force_quotes,                                                                                                                                                                                                                                                                            
        :headers => @export_columns.collect { |column| format_export_column_header_name(column) }                                                                                                                                                                                                                                
      }                                                                                                                                                                                                                                                                                                                          

      data = FasterCSV.generate(fcsv_options) do |csv|                                                                                                                                                                                                                                                                           
        csv << fcsv_options[:headers] unless params[:skip_header] == 'true'                                                                                                                                                                                                                                                      
        @records.each do |record|                                                                                                                                                                                                                                                                                                
          csv << @export_columns.collect { |column|                                                                                                                                                                                                                                                                              
            # Convert to UTF-16 discarding the BOM, required for Excel (> 2003 ?)                                                                                                                                                                                                                                     
            Iconv.conv('UTF-16', 'UTF-8', get_export_column_value(record, column))[2..-1]                                                                                                                                                                                                                                        
          }                                                                                                                                                                                                                                                                                                                      
        end                                                                                                                                                                                                                                                                                                                      
      end                                                                                                                                                                                                                                                                                                                        
    -%><%= data -%>

The important line is:

Iconv.conv('UTF-16', 'UTF-8', get_export_column_value(record, column))[2..-1]
-2

open the file csv with notepad++ clic on Encode, select convert to UTF-8 (not convert to UTF-8(without BOM)) Save open by double clic with excel Hope that help Christophe GRISON

1
  • 3
    This doesn't answer the question as its supposed to be done programmatically and not require user intervention to manually re-save every file
    – Joe W
    Apr 25, 2013 at 17:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.