Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php header excel and utf-8

Tags:

php

utf-8

ob_start();

echo 'Désçàui';

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type:   application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment; filename=Test.xls"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); 

ob_end_flush();

What I'm getting in the excel file is Désçàui

However, I do get Désçàui when I try

ob_start();
echo 'Désçàui';
header("Content-Type:   text/html; charset=utf-8");
ob_end_flush();

Any help experts?

PS. The file is saved in DW with Title/Encoding Unicode(Utf-8).

like image 767
Jeremy Roy Avatar asked Mar 20 '11 11:03

Jeremy Roy


3 Answers

Source http://www.mwasif.com/2007/5/download-data-csv-using-php/

Solution worked for me

Danish Zahur said,

October 7, 2009 @ 7:23 pm

If your contents are in UTF-8 format, then no need to convert encoding. Just start your file/output stream with UTF-8 BOM after headers.

echo pack("CCC",0xef,0xbb,0xbf);

And header should contain encoding UTF-8

header( "Content-type: application/vnd.ms-excel; charset=UTF-8" );

It will work like charm because Excel will recognize file charset with BOM bytes.

like image 93
Asif Avatar answered Oct 27 '22 18:10

Asif


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');
like image 20
Dr.Molle Avatar answered Oct 27 '22 18:10

Dr.Molle


I don't know how you're generating the excel file. But, if you're doing it from an HTML output, you can just add the following at the begining:

<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />

Regards

like image 20
gonetil Avatar answered Oct 27 '22 19:10

gonetil