php 标头 excel 和 utf-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5368150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:11:12  来源:igfitidea点击:

php header excel and utf-8

phputf-8

提问by Jeremy Roy

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();

What I'm getting in the excel file is D??s?§??ui

我在excel文件中得到的是 D??s?§??ui

However, I do get Dés?àui when I try

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

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

Any help experts?

任何帮助专家?

PS. The file is saved in DW with Title/Encoding Unicode(Utf-8).

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

采纳答案by Dr.Molle

I'm not sure, but it may be that excel can not handle utf8(may depend on the version). But it can handle utf16, so try converting the charset. This works for me(in excel2002):

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

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

回答by Asif

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

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

Solution worked for me

解决方案对我有用

Danish Zahur said,

丹麦扎胡尔说,

October 7, 2009 @ 7:23 pm

2009 年 10 月 7 日下午 7:23

If your contents are in UTF-8 format, then no need to convert encoding. Just start your file/output stream with UTF-8 BOM after headers.

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

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

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

And header should contain encoding UTF-8

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

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

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

It will work like charm because Excel will recognize file charset with BOM bytes.

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

回答by gonetil

I don't know how you're generating the excel file. But, if you're doing it from an HTML output, you can just add the following at the begining:

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

<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />

<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />

Regards

问候

回答by Mladen Janjetovic

This UTF-8 BOM line of code made my UTF-8 chars work fine:

这个 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;

回答by Smokie

Converting utf8 to html-entities worked quite nicely for me :

将 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 ; 

回答by Unicron

The content-typeheaders are relevant for the browser only. They have no effect on downloaded files. Once the file is saved, it is up to the application to decide how it treats the data in the file.

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

The example you show is not a valid Excel file in the first place. When encountering what it must think is a broken file, Excel probably switches to some default processing that assumes windows-1252or some other single-byte character set.

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

You would have to give Excel a proper file to open. Alternatively, it may be possible to use the old "Output HTML but save as XLS" trick and specify a UTF-8 encoding in that HTML file.

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

回答by Manee Mana

try this

尝试这个

<?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>";

?>

回答by EkoDrive

Try this:

尝试这个:

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);