将php输出导出为ex​​cel

edi*_*dib 2 php character-encoding export-to-excel

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();  
} 
Run Code Online (Sandbox Code Playgroud)

这是我的export2excel.php文件,func1,2,3在functions.php文件中并生成表输出,除了字符编码以外都很有效.我对我的所有文件使用utf-8编码.第二个,如果上面的语句产生健康的编码输出,但是休息2用我喜欢的字符编码我的输出,如"BÜTÇEİÇİ".土耳其语中的"BÜTÇEİÇİ".

简而言之.相同的文件,相同的编码,相同的数据库但结果不同.

任何的想法?

Ben*_*mer 19

Excel使用UTF-16LE + BOM作为默认Unicode编码.
因此,您必须将输出转换为UTF-16LE并添加UTF-16LE-BOM "\xFF\xFE".

更多信息:

相反,我会使用现有的库之一

编辑:
一些代码,可以帮助,如果你真的不希望使用现有的库

<?php
$output = <<<EOT
<table>
    <tr>
        <td>Foo</td>
        <td>IñtërnâtiônàlizætiøöäÄn</td>
    </tr>
    <tr>
        <td>Bar</td>
        <td>??????? ???????? ?????? ? ????????</td>
    </tr>
</table>
EOT;

// Convert to UTF-16LE
$output = mb_convert_encoding($output, 'UTF-16LE', 'UTF-8'); 

// Prepend BOM
$output = "\xFF\xFE" . $output;

header('Pragma: public');
header("Content-type: application/x-msexcel"); 
header('Content-Disposition: attachment;  filename="utf8_bom.xls"');

echo $output;
Run Code Online (Sandbox Code Playgroud)