将 PHP 导出到 Excel

3

我遇到了一个问题,无法将日语文本导出到Excel(xls)中。

我使用了以下代码:

header('Content-type: application/ms-excel;charset=UTF-8');
header('Content-Disposition: attachment; filename='.$filename); 
header("Pragma: no-cache");     
echo $contents;

但在 Excel 文件中,文本变成了像这样的有趣字符:
é™?定ç‰? ã?¨ã??ã?¯ã??ã?£ã?†ã?ªã?¢å??犬ã?®ã?Œæ??ã? 

’è??ã??ã?Ÿã?†ã?£ã??ã??ã??ã?? ï??

目前,我正在使用HostingManager,并且我在不同的服务器上尝试了相同的代码,没有任何问题。

可能是什么问题。由于PHP版本?请帮帮我。


1
你从哪里获取$contents的内容? - John V.
我从数据库中获取变量并将它们全部放入该变量($contents)中。 $contents .= order_id"."\t"."$fname"."\t"."$lname"."\t"."$email"."\t"; - Aino
你有考虑过发送一个真正的 Excel 负载/文件,而不仅仅是伪装成 CSV 吗? - mario
可能是"Warning: Headers already sent" in PHP的重复问题。 - mario
两个服务器上的数据库相同吗? - John V.
这被称为乱码。这段文本原本应该是什么?我认为你使用的是“sjis”编码,而不是“utf-8”编码,可能与乱码一起出现。 - Rick James
2个回答

1
尝试这个。
<form action="itemexcel.php" method="post" 
onsubmit='$("#datatodisplay").val( $("<div>").append( $("#ReportTable")
.eq(0).clone()).html() )'>
或无论我们想使用哪个,
<table id="ReportTable" width="781" border="2"> Or <div id id="ReportTable">
<tr>
<td><input type="hidden" id="datatodisplay" name="datatodisplay"></td>
<td><input class="bg" type="submit" value="Export To Excel"></td>
</tr></table></div>

<input type="hidden" id="datatodisplay" name="datatodisplay">
<input class="bg" type="submit" value="Export To Excel">

itemexcel.php页面

<?php
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=itemcode.xls');
// Fix for crappy IE bug in download.
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['datatodisplay'];
?>
的id应为"ReportTable"。

0
header("Content-type: application/vnd.ms-excel;charset=UTF-8"); 
header("Content-Disposition: attachment; filename=\"download.xlsx");
header("Cache-control: private");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接