Web Development Blog

24K Design Studio :: Blog :: Using PHP, how to generate UTF8 encoded CSV file for Excel.
Author:
Password:
Using PHP, how to generate UTF8 encoded CSV file for Excel.
02:39, 07 Jul, 2011
by David

As we know, there is a function in PHP called fputcsv that can transfer array into CSV line.

However, if you make a CSV file only with these CSV lines, containing UTF8 characters, the generated CSV file will became a chaos in Excel.

 

Solution example:

$fp = fopen("php://temp", 'r+');
fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
fputcsv($fp, ...... 

(I got to know this idea from blog.foolbird.net/1517.html)

 

Reason: the 0xEF 0xBB 0xBF BOM header will let Excel know the correct encoding.

Tags: PHP Excel UTF8
Reply:
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#1
Alex
22:28, 12 Jan, 2012
 
Thank you! Been searching for this solution for hours
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#1
xfphqube
05:03, 25 Nov, 2015
 
1
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#2
massimo71
16:19, 04 Jul, 2012
 
thank you MAN !
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#1
qagktkyp
05:03, 25 Nov, 2015
 
1
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#3
Peter
21:37, 12 Jul, 2012
 
Thanks!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#1
wkwopwos
05:03, 25 Nov, 2015
 
1
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#4
Johan
03:58, 22 Aug, 2012
 
Thanks for the great tip! Helped me out really quickly.
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#5
Ricardo
04:32, 11 Oct, 2012
 
You saved my ass man!

I've been search for this for days.

THANK YOU SO MUCH!.
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#6
Sameer Singh Aswal
18:39, 25 Oct, 2012
 
THANKS A LOT....................
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#7
shihabudheenf
20:09, 31 Oct, 2012
 
wonderful ll....thank you very much...once again thank you ........
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#8
meshi
19:44, 15 Nov, 2012
 
hi tried this one and it works for me. problem is whe i have comma in my text it will go to the next cell. would it be possible to treat the comma as text and delimeter?
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#9
Misho
20:51, 10 Dec, 2012
 
Thank you very much :)
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#10
Enomatix
18:58, 19 Dec, 2012
 
You have saved the day. Thank you very much
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#11
Anonymous
22:44, 04 Jan, 2013
 
Great tip, solved my problem!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#12
Ajon
16:06, 22 Jan, 2013
 
Respect!!! You seve my brain!!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#13
sanja_nvkz
22:49, 13 Mar, 2013
 
Thank you!!!!!!!!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#14
JB
06:21, 22 Mar, 2013
 
what can I say, thank you so much, I would have spent hours on this problem without this article, kudos to you!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#15
Roberto Castillo
08:00, 18 Jun, 2013
 
Excellent
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#16
Secus
20:28, 08 Oct, 2013
 
Still useful for me, thanks !
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#17
Gabriel
18:10, 04 Mar, 2014
 
THANKKKSSSS
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#18
Felix
19:07, 03 Apr, 2014
 
thanks brother
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#19
frank
13:36, 06 May, 2014
 
Thank you so much. Greek is now possible
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#20
Gazar
15:57, 20 May, 2014
 
Thank you so much
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#21
Nguyên
15:59, 12 Jun, 2014
 
Thanks so much
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#22
Guru
17:34, 15 Jul, 2014
 
<?php
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=spreadsheet.xls");
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$q=mysql_query("select * from table1") or die(mysql_error());
if(mysql_num_rows($q)>0)
{
echo"<table><tr><th>Name</th><th>Address</th><th>Phone</th></tr>";
while($res=mysql_fetch_array($q))
{
echo"<tr><td>".$res['name']."</td><td>".$res['address']."</td><td>".$res['phone']."</td></tr>";
}
echo"</table>";
}
?>

Reference: http://www.phponwebsites.com/2014/07/php-create-excel-sheet.html
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#23
mrPHP
22:06, 12 Sep, 2014
 
OMG.. been searching for a fix for days! THANK YOU!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#24
Muhsin
05:31, 20 Oct, 2014
 
Thank you!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#25
Neng
15:23, 14 Jan, 2015
 
Thank You So Much.
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#26
guracao
15:50, 25 Jun, 2015
 
Thanks man! Works too for this:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=xyz.csv");
just:
echo chr(0xEF).chr(0xBB).chr(0xBF);
and everything goes well
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
#27
Daiyrbek
16:08, 26 Apr, 2016
 
Dude, thanks a lot!
Reply
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

Reply
View the latest 5 entries.