views:

134

answers:

2
include_once 'mysqlconn.php';
include_once "functions.php";
$filename = $_GET['par'].".xls";
header("Content-type: application/x-msexcel"); 
header('Content-Disposition: attachment; filename="'.basename($filename).'"'); 
if ($_GET['i'] == "par1") {
  func1();
} else if ($_GET['i'] == "par2") {
  echo "şşşıııİİİ";
  func2();  
} else if ($_GET['i'] == "par3") {  
  echo "şşşıııİİİ";
  func3();  
} 

this is my export2excel.php file and func1,2,3 are in functions.php file and produces table output all work well except character encoding in a strange way. I am using utf-8 encoding for all my files. 2nd else if statement above produces healthy encoded output but rest 2 are encodes my output with strange characters like "BÃœTÇE İÇİ". it is "BÜTÇE İÇİ" in turkish.

in short. same files, same encoding, same database but different results.

any idea?

A: 

Excel uses UTF-16LE as the default encoding. So you should either convert UTF-8 to UTF-16LE yourself or use one of the tried and tested Excel PHP libs instead of trying to reinvent the wheel. I would recommend using PHPExcel...

wimvds
I have only 8 pages to convert excel. and as I read from docs, phpexcel classes can only convert cell by cell conversions but not raw php html output. so I will have to recode all things for phpexcel and it classes' size is 7 mB. It is unnecassary I think.
edib
I'm not sure exactly what you're trying to do, but PHPExcel can generate "raw" html output... as for the library size, I've commented on that elsewhere on SO.. Note that 4.5 MB of that 7MB is the tcpdf library that PHPExcel includes to support export to PDF... if you don't need PDF, then delete it, and the disk footprint is less than 3MB
Mark Baker
+2  A: 

Excel uses UTF-16LE + BOM as default Unicode encoding.
So you have to convert your output to UTF-16LE and prepend the UTF-16LE-BOM "\xFF\xFE".

Some further information:

Instead I would use one of the existing libraries

Edit:
Some code that could help if you really not want to use an existing library

<?php
ob_start();
?>
<table>
    <tr>
        <td>şşşıııİİİ</td>
        <td>bar</td>
    </tr>
    <tr>
        <td>foo</td>
        <td>şşşıııİİİ</td>
    </tr>
</table>
<?php
$output = ob_get_clean();
// Convert to UTF-16LE
$output = mb_convert_encoding($output, 'UTF-16LE', 'UTF-8'); 

$fh = fopen('foo.xls', "w");

 // Prepend BOM
fwrite($fh, "\xFF\xFE");
fwrite($fh, $output);
fclose($fh);

header("Content-type: application/x-msexcel"); 
header('Content-Disposition: attachment;  filename="foo.xls"');
readfile('foo.xls');  
?>
Benjamin Cremer
thanks, that is exactly what I want.
edib
In windows everything works fine but In redhat linux line fwrite($fh, b"\xFF\xFE"); produces error: "PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /var/www/...." why is it so?
edib
when I change the code like this it worked.$fh = fopen('foo.xls', "wb"); // Prepend BOM fwrite($fh, "\xFF\xFE");
edib
Was a typo... now fixed.
Benjamin Cremer