11

In MySQL I have set my data field type to utf8_bin and i am storing data in Unicode. Texts are being properly displayed in web pages.

I want to generate excel file exporting data from my table to it. The output in .xls and .cvs is - '????'.

I checkout out other answers here, its been referred to use headers:

header("content-type:application/csv;charset=UTF-8");

similar question. But its not working. After using header, in csv Output is - सूरà¥à¤¯à¤¾.

Please help. Thanks.

4
  • @Mark Baker yes i have read about it in some posts but i am not sure how to use it. Can you help. Commented Jul 11, 2013 at 12:15
  • Before outputting any of your data to the file, write "\xEF\xBB\xBF" to the file...it may also help to write a line "sep=," after the BOM and before the data (or appropriate character if you're not using a comma) because MS Excel's default separator is locale-specific Commented Jul 11, 2013 at 12:21
  • 1
    The ‘similar question’ has example code. Putting a fake-BOM in a UTF-8 file is almost always the Wrong Thing, but it is necessary for getting Excel to read UTF-8 CSV unfortunately. Commented Jul 11, 2013 at 12:32
  • Possible duplicate of How can I output a UTF-8 CSV in PHP that Excel will read properly? Commented Apr 11, 2018 at 20:54

4 Answers 4

21

This post solved my problem: https://stackoverflow.com/a/4762708/2349494

And this is what did the conversion:

print chr(255) . chr(254) . mb_convert_encoding($csv_output, 'UTF-16LE', 'UTF-8');

Earlier I was getting garbage characters now correct data is exported.

Sign up to request clarification or add additional context in comments.

4 Comments

Note that the resulting csv will be twice the size of a UTF8 one. A small price to pay for a very easy solution to a hairy problem that has been plaguing many people for a long while !
A small finding I would like to add here is, if you are doing tsv rather than csv then add \t using double quotes "" not single quotes ''
THANK YOU! This tip saved my day.
Unfortunately for me roughly 75% of the resulting text is in Chinese characters, the remaining 25% is shown correctly. What could this mean?
13

I had the same problem and I did it by combining two things:

First, you have to change table collation to UTF8 e.g: "utf8_general_ci" (or just make sure of that), then add this code after your MySQL query:

mysql_query("SET NAMES utf8");

Like

$result = mysql_query("SHOW COLUMNS FROM table WHERE Field NOT IN ('user_id', 'password')");
mysql_query("SET NAMES utf8");

And then, use this as header (adapt with your needs):

header('Content-Description: File Transfer');
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
header('Content-Transfer-Encoding: binary');
header('Pragma: public');
print "\xEF\xBB\xBF"; // UTF-8 BOM
print $csv_output;
exit;

5 Comments

din't worked my friend. Showing garbage characters in elx/csv file. I have put all code here. Plz check - oxyjone.blogspot.in/2013/07/generating-excel-having-utf-8.html
I think its my bad! mysql_query("SET NAMES utf8"); should be befor main query! :)
tip: the "\xEF\xBB\xBF" BOM gives you an error while using nginx, you can't force download using it. hours wasted on that.
This line solve the problem print "\xEF\xBB\xBF"; // UTF-8 BOM
@Husseinmahyoub same for me
6

I have a variant to Sajal response and found it on php.net at : http://www.php.net/manual/en/function.iconv.php#104287

Echo this at the begining of the content of the csv :

chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $data)

I prefer with iconv() but it seems it do the same as mb_convert_encoding(). Don't forget to replace ; by tabs (\t) and it worked fine for me like this !

Comments

0

Try following code;

ob_end_clean();
$_filename = date('Y_m_d_H_i').".csv";

header("Cache-Control: public"); 
header("Content-Type: application/octet-stream");
header("Content-Type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=$_filename");
// Read the file from disk
readfile($filename);
exit();

1 Comment

Please refer this http://stackoverflow.com/questions/4348802/how-can-i-output-a-utf-8-csv-in-php-that-excel-will-read-properly?rq=1

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.