【问题标题】:php header excel and utf-8php 标头 excel 和 utf-8
【发布时间】:2011-07-19 02:19:33
【问题描述】:
ob_start();

echo 'Désçàui';

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type:   application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment; filename=Test.xls"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); 

ob_end_flush();

我在 excel 文件中得到的是Désçàui

但是,当我尝试时,我确实得到了 Désçàui

ob_start();
echo 'Désçàui';
header("Content-Type:   text/html; charset=utf-8");
ob_end_flush();

任何帮助专家?

PS。该文件以标题/编码 Unicode(Utf-8) 保存在 DW 中。

【问题讨论】:

  • 一个实体只能有一种内容类型。
  • 我认为为甚至不是文本的 MIME 类型提供 charset 属性是没有意义的。
  • 从什么时候开始将纯文本块视为 Excel 文件?

标签: php utf-8


【解决方案1】:

试试这个:

header('Content-Transfer-Encoding: binary');
header("Content-Type: application/octet-stream"); 
header("Content-Transfer-Encoding: binary"); 
header('Expires: '.gmdate('D, d M Y H:i:s').' GMT'); 
header('Content-Disposition: attachment; filename = "Export '.date("Y-m-d").'.xls"'); 
header('Pragma: no-cache'); 

//these characters will make correct encoding to excel 
echo chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $out); 

【讨论】:

    【解决方案2】:

    来源http://www.mwasif.com/2007/5/download-data-csv-using-php/

    解决方案对我有用

    丹麦语 Zahur 说,

    2009 年 10 月 7 日下午 7:23

    如果您的内容是 UTF-8 格式,则无需转换编码。只需在标头后使用 UTF-8 BOM 启动文件/输出流即可。

    echo pack("CCC",0xef,0xbb,0xbf);

    并且标头应该包含编码 UTF-8

    header( "Content-type: application/vnd.ms-excel; charset=UTF-8" );

    它会像魅力一样工作,因为 Excel 将识别带有 BOM 字节的文件字符集。

    【讨论】:

    • 我不明白这到底是怎么回事!谢谢!!
    • 如果我添加“回声包”的东西,字符实际上显示得很好,但是所有项目都在一个 excel 单元格中,如果我删除它,字符显示为未知符号,但每个项目数组与预期的一样位于不同的单元格中
    • 这是目前可行的解决方案(截至 2020 年)。非常感谢!
    【解决方案3】:

    这段 UTF-8 BOM 代码行让我的 UTF-8 字符工作正常:

     header("Content-type: application/vnd.ms-excel");
     header("Content-Disposition: attachment; filename='".$file_name."'");
     header("Pragma: no-cache");
     header("Expires: 0");
     echo "\xEF\xBB\xBF"; //UTF-8 BOM
     echo $out;
    

    【讨论】:

    • 这是一个使用 Excel 2016 轻而易举的解决方案。感谢 Mladen Janjetovic!
    【解决方案4】:

    试试这个

    <?php
    header("Content-Type: application/vnd.ms-excel");
    header('Content-Disposition: attachment; filename="sample.xls"');
    echo "
        <html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">
        <html>
            <head><meta http-equiv=\"Content-type\" content=\"text/html;charset=utf-8\" /></head>
            <body>
    ";
    
    echo "
        <table>
          <tr>
            <th rowspan=\"2\" nowrap=\"nowrap\">เลขที่บัญชี</th>
            <th rowspan=\"2\" nowrap=\"nowrap\">ชื่อ-สกุล ลูกค้า</th>
            <th rowspan=\"2\" nowrap=\"nowrap\">OS/Balance</th>
            <th rowspan=\"2\" nowrap=\"nowrap\">วันที่</th>
            <th rowspan=\"2\" nowrap=\"nowrap\">เวลา</th>
            <th rowspan=\"2\" nowrap=\"nowrap\">Action Code</th>
            <th rowspan=\"2\" nowrap=\"nowrap\">Amount</th>
            <th colspan=\"5\" nowrap=\"nowrap\">ผลการติดตาม</th>
          </tr>
          <tr>
            <th nowrap=\"nowrap\">ที่อยู่บ้าน</th>
            <th nowrap=\"nowrap\">เบอร์โทรบ้าน</th>
            <th nowrap=\"nowrap\">เบอร์โทรมือถือ</th>
            <th nowrap=\"nowrap\">ที่อยู่ที่ทำงาน</th>
            <th nowrap=\"nowrap\">เบอร์โทรที่ทำงาน</th>
          </tr>
          <tr>
            <td>acc</td>
            <td>name</td>
            <td>balance</td>
            <td>date</td>
            <td>time</td>
            <td>code</td>
            <td>amount</td>
            <td>h-addr</td>
            <td>h-tel</td>
            <td>cell</td>
            <td>w-addr</td>
            <td>w-tel</td>
          </tr>
        </table>
    ";
    
    echo "</body></html>";
    
    ?>
    

    【讨论】:

    • 嗨!如果我添加 - "echo "\xEF\xBB\xBF"; //UTF-8 BOM" 你的例子对我有用,那么它就很好用。但是你知道在没有空格的情况下在excel中制作普通表格吗?
    【解决方案5】:

    将 utf8 转换为 html-entities 对我来说效果很好:

    $str = 'utf-string ...';
    if (mb_detect_encoding($str ) == 'UTF-8') {
       $str = mb_convert_encoding($str , "HTML-ENTITIES", "UTF-8");
    }
    
    header('Content-type: application/x-msdownload; charset=utf-16');
    header('Content-Disposition: attachment; filename=companies.xls');
    header('Pragma: no-cache');
    header('Expires: 0');
    
    echo $str ; 
    

    【讨论】:

      【解决方案6】:

      我不知道您是如何生成 excel 文件的。但是,如果您是从 HTML 输出中执行此操作,则只需在开头添加以下内容:

      &lt;meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" /&gt;

      问候

      【讨论】:

        【解决方案7】:

        我不确定,但可能是excel无法处理utf8(可能取决于版本)。但它可以处理 utf16,所以尝试转换字符集。 这对我有用(在 excel2002 中):

        echo mb_convert_encoding('Désçàui','utf-16','utf-8');
        

        【讨论】:

        • echo mb_convert_encoding($r-&gt;lastname,'utf-16','utf-8');
        • 谢谢您,先生。 7 年后仍然有用。
        【解决方案8】:

        content-type 标头仅与浏览器相关。它们对下载的文件没有影响。保存文件后,由应用程序决定如何处理文件中的数据。

        您展示的示例首先不是有效的 Excel 文件。当遇到它必须认为是损坏的文件时,Excel 可能会切换到一些默认处理,假定 windows-1252 或其他一些单字节字符集。

        您必须为 Excel 提供一个正确的文件才能打开。或者,可以使用旧的“输出 HTML 但另存为 XLS”技巧并在该 HTML 文件中指定 UTF-8 编码。

        【讨论】:

        • 您好,请问您是如何创建excel文件的?谢谢独角兽。
        • @Jeremy Roy - 有许多库允许您从 PHP 创建真正的 Excel 文件。就个人而言,我推荐 PHPExcel
        • @Unicorn 您能否将一个示例与那个丑陋(但有效)的解决方案联系起来?我说的是生成一个 HTML 文件,但将其下载为 xls。谢谢
        猜你喜欢
        • 1970-01-01
        • 2010-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-23
        相关资源
        最近更新 更多