【问题标题】:PHP export to excelPHP导出到excel
【发布时间】:2012-11-30 05:01:07
【问题描述】:

我遇到了无法将日文文本导出到 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版本?? 请帮帮我。

【问题讨论】:

  • 你从哪里得到$contents的内容?
  • 我从数据库中获取变量并将所有变量放入该变量($contents)中。 $contents .= order_id"."\t"."$fname"."\t"."$lname"."\t"."$email"."\t";
  • 您是否考虑过发送实际的 excel 有效负载/文件,而不是伪装成 CSV?
  • 两台服务器上的同一个数据库?

标签: php export-to-excel


【解决方案1】:

试试这个

<form action="itemexcel.php" method="post" 
onsubmit='$("#datatodisplay").val( $("<div>").append( $("#ReportTable")
.eq(0).clone()).html() )'>

div 或 table 我们想要使用的 div 或 table 应该是 id="ReportTable"

<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'];
?>

【讨论】:

    【解决方案2】:
    header("Content-type: application/vnd.ms-excel;charset=UTF-8"); 
    header("Content-Disposition: attachment; filename=\"download.xlsx");
    header("Cache-control: private");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      相关资源
      最近更新 更多