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



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

i've got simple thing outputs stuff in csv format, it's got utf-8. open file in textedit or textmate or dreamweaver , displays utf-8 characters properly, if open in excel it's doing silly íÄ kind of thing instead. here's i've got @ head of document:

header("content-type:application/csv;charset=utf-8"); header("content-disposition:attachment;filename=\"chs.csv\"");

this seems have desired effect except excel (mac, 2008) doesn't want import properly. there's no options in excel me "open utf-8" or anything, … i'm getting little annoyed.

i can't seem find clear solutions anywhere, despite lot of people having same problem. thing see include bom, can't figure out how that. can see above i'm echoing data, i'm not writing file. can if need to, i'm not because there doesn't seem need @ point. help?

update: tried echoing bom echo pack("ccc", 0xef, 0xbb, 0xbf); pulled site trying observe bom. excel appends 3 characters first cell when imports, , still messes special characters.

to quote a microsoft back upwards engineer,

excel mac not back upwards utf-8

in order output utf-8 content excel both on windows , os x able read, need 2 things:

make sure convert utf-8 text utf-16le

mb_convert_encoding($csv, 'utf-16le', 'utf-8');

make sure add together utf-16le byte order mark

chr(255) . chr(254)

the next problem appears excel on os x (but not windows) when viewing csv file comma separated values, excel render rows 1 row , of text along commas in first row.

the way avoid utilize tabs separated value.

i used this function php comments (using tabs "\t" instead of commas) , worked on os x , windows excel.

note prepare issue empty column end of row, did have alter line of code says:

$field_cnt = count($fields);

to

$field_cnt = count($fields)-1;

as of other comments on page say, other spreadsheet apps openoffice calc, apple's own numbers , google doc's spreadsheet have no issues utf-8 files commas.

see the table in question works , doesn't work unicode csv files in excel

as side note, might add together if using composer, should have @ adding league\csv requires. league\csv has a nice api building csv files.

to utilize league\csv method of creating csv files, check out this example

php csv utf-8 byte-order-mark

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -