PHP Header Excel and Utf-8

php header excel and utf-8

I'm not sure, but it may be that excel can not handle utf8(may depend on the version). But it can handle utf16, so try converting the charset.
This works for me(in excel2002):

echo mb_convert_encoding('Désçàui','utf-16','utf-8');

How can I output a UTF-8 CSV in PHP that Excel will read properly?

To quote a Microsoft support engineer,

Excel for Mac does not currently support UTF-8

Update, 2017: This is true of all versions of Microsoft Excel for Mac before Office 2016. Newer versions (from Office 365) do now support UTF-8.

In order to output UTF-8 content that Excel both on Windows and OS X will be able to successfully read, you will need to do two things:

  1. Make sure that you convert your UTF-8 CSV text to UTF-16LE

    mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');
  2. Make sure that you add the UTF-16LE byte order mark to the start of the file

    chr(255) . chr(254)

The next problem that appears only with Excel on OS X (but not Windows) will be when viewing a CSV file with comma separated values, Excel will render rows only with one row and all of the text along with the commas in the first row.

The way to avoid this is to use tabs as your separated value.

I used this function from the PHP comments (using tabs "\t" instead of commas) and it worked perfectly on OS X and Windows Excel.

Note that to fix an issue with an empty column as the end of a row, that I did have to change the line of code that says:

    $field_cnt = count($fields);

to

    $field_cnt = count($fields)-1;

As some of the other comments on this page say, other spreadsheet apps like OpenOffice Calc, Apple's own Numbers and Google Doc's Spreadsheet have no issues with UTF-8 files with commas.

See the table in this question for what works and doesn't work for Unicode CSV files in Excel


As a side note, I might add that if you are using Composer, you should have a look at adding League\Csv to your requires. League\Csv has a really nice API for building CSV files.

To use League\Csv with this method of creating CSV files, check out this example

PHPEXCEL set UTF-8 encode

What is wrong with my headers ?

Nothing, your headers are looking fine.

What is wrong with Excel?

The user who opens the file in Excel needs to tell Excel that the file is in UTF-8 encoding. Direct that user to contact the vendor of the software it uses for her/his support options.

Users of LibreOffice or a derivate of it do not have that problem btw., so one solution is to tell those to install a suite of such, open the CSV file and save it as Excel file for example.

Or you directly create the Excel file on your server.

export excel file in php with utf-8 unicode

I use PHPExcel plugin for codeigniter. it works perfect for utf-8

Using PHPExcel to store utf-8 characters to a file

'–'

is a HTML Number.

Use it like this to get the original value:

echo html_entity_decode('–', ENT_COMPAT, 'UTF-8');

Hope this helps.

PHP output Issues with utf-8 characters

You should check the character encoding of the xlsx file. If the file was created on windows then it may have the Windows-1252 (CP1252) character encoding. If so then it needs to be converted to UTF-8. See the documentation on how to handle the character encoding of excel files. Following should be useful:

https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md#reading-a-csv-file and

https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md#writing-utf-8-csv-files

https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md#writing-utf-8-html-files

Also see this related issue: How can I output a UTF-8 CSV in PHP that Excel will read properly?



Related Topics



Leave a reply



Submit