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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解