duanbi3151 2010-08-12 08:05
浏览 61
已采纳

将php输出导出为ex​​cel

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?

  • 写回答

3条回答 默认 最新

  • dongliqin6939 2010-08-12 08:57
    关注

    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
    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多