【问题标题】:PHP Export mysql data to excel sheet but display in browserPHP将mysql数据导出到excel表但在浏览器中显示
【发布时间】:2016-05-11 10:23:43
【问题描述】:

我正在尝试将数据从 PHP mysql 导入到 excel 中,并且运行良好。但是一旦我点击导入按钮,它就会显示在浏览器中。

header("Content-Disposition: attachment; filename=faculty-export.xls");
header("Content-type: application/vnd-ms-excel; charset=UTF-8;");

我已经在我的 PC(Windows 7、IIS、PHP 5.4)上进行了本地测试,它工作正常,但是当我在 Web 服务器(PHP 5.3、Linux、Apache 2.2.3)上上传时,导出数据显示在浏览器中(测试过的浏览器) IE 10、11、Firefox 和 Chrome)。

我遇到了问题,因为我们必须更改服务器中的某些设置,或者我必须对我的编码中的某些内容做些什么?

发布数据

 <form method="post" action="export_excel.php">
<input type="hidden" name="excel_name" value="<?php echo $_GET['name']; ?>">
<input type="hidden" name="excel_gender" value="<?php echo $_GET['gender']; ?>">
<input type="hidden" name="excel_college" value="<?php echo $ColName; ?>">
<input type="hidden" name="excel_department" value="<?php echo $_GET['deptf']; ?>">
<input type="hidden" name="excel_nationality" value="<?php echo $_GET['natf']; ?>">
<input type="hidden" name="excel_finalstatus" value="<?php echo $_GET['fstatusf']; ?>">
<input type="hidden" name="excel_fyear" value="<?php echo $_GET['fyearf']; ?>">
  <input style="float:left;" type="submit" value="Save to Excel">
</form>

export_excel.php 的代码如下。

<?php
  error_reporting(0);

  include("connectdb.php"); 
  if (empty($_POST['excel_name']) && empty($_POST['excel_gender']) && empty($_POST['excel_college']) && empty($_POST['excel_department']) && empty($_POST['excel_nationality']) && empty($_POST['excel_finalstatus']) && empty($_POST['excel_fyear']))
{
    /*$query="select * from ab";*/
    $query="select * from ab";

}
 else
    {
         $wheres = array();

          $query = "select * from ab where ";

   if (isset($_POST['excel_name']) and !empty($_POST['excel_name']))
{
    $wheres[] = "name like '%{$_POST['excel_name']}%' ";
} 

if (isset($_POST['excel_gender']) and !empty($_POST['excel_gender']))
{
    $wheres[] = "gender = '{$_POST['excel_gender']}'";
} 

if (isset($_POST['excel_college']) and !empty($_POST['excel_college']))
{
    $wheres[] = "college = '{$_POST['excel_college']}' ";
} 

if (isset($_POST['excel_department']) and !empty($_POST['excel_department']))
{
    $wheres[] = "department = '{$_POST['excel_department']}' ";
} 

if (isset($_POST['excel_nationality']) and !empty($_POST['excel_nationality']))
{
    $wheres[] = "nationality = '{$_POST['excel_nationality']}' ";
} 

if (isset($_POST['excel_finalstatus']) and !empty($_POST['excel_finalstatus']))
{
    $wheres[] = "finalstatus = '{$_POST['excel_finalstatus']}' ";
}

if (isset($_POST['excel_fyear']) and !empty($_POST['excel_fyear']))
{
    $wheres[] = "fyear = '{$_POST['excel_fyear']}' ";
} 

foreach ( $wheres as $where ) 
{
  $query .= $where . ' AND ';   //  you may want to make this an OR
 }
   $query=rtrim($query, "AND "); 

  }

   $result = mysqli_query($conn,$query);

    $xls_filename = 'export_'.date('d-m-Y').'.xls'; // Define Excel (.xls) file name


 header("Pragma: public");
 header("Expires: 0");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Content-Type: application/force-download");
 header("Content-Type: application/octet-stream");
 header("Content-Type: application/download");;
 header("Content-Disposition: attachment; filename=$xls_filename");
 header("Content-Transfer-Encoding: binary "); 



echo "<table border='1'><tr><th>الإسم</th><th>تاريخ وصول الملف</th><th>الكلية</th><th>القسم</th><th>العمر</th><th>الجنسية</th><th>الجنس</th><th>الدرجة</th><th>نوع التعيين</th><th>السنة</th><th>البكالوريوس<br>سنة التخرج</th><th>الماجستير<br>سنة التخرج</th><th>الدكتوراه<br>سنة التخرج</th><th>الملاحظات</th><th>تاريخ اللجنة</th>";





while($res = mysqli_fetch_assoc($result)){ 
echo "<tr><td>".$res['name']."</td><td>".$res['DOA']."</td><td>".$res['college']."</td><td>".$res['department']."</td><td>".$res['DOB']."</td><td>".$res['nationality']."</td><td>".$res['gender']."</td><td>".$res['fwtype']."</td><td>".$res['finalstatus']."</td><td>".$res['fyear']."</td>";

$query2 = "SELECT * FROM abc where ".$res['id']." = DId";
$result1 = mysqli_query($conn,$query2);

if (mysqli_num_rows($result1) > 0) {
    while($row = mysqli_fetch_assoc($result1)) {
    echo "<td>".$row['DUName']."<br>".$row['DYEAR']."</td>";    
    }
    }
    }
  echo "</tr></tr></table>";


    exit();

 ?>

【问题讨论】:

  • 查看更新的问题
  • 尝试启用 mod_mime 如果你还没有,然后在你的 apache 配置中适当的地方插入 application/vnd-ms-excel 的 AddType 指令。
  • 抱歉最近的回复。 MIME 类型已启用。

标签: php mysql excel import


【解决方案1】:

你好,我在这里改变了一些东西

<?php
$xls_filename = 'export_'.date('d-m-Y').'.xls'; // Define Excel (.xls) file name
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$xls_filename");
header("Content-Transfer-Encoding: binary ");

include("connectdb.php");
$query="select * from ab WHERE 1=1 ";
if (isset($_POST['excel_name']) and !empty($_POST['excel_name']))
 {
  $query.= " AND name like '%{$_POST['excel_name']}%' ";
 }
if (isset($_POST['excel_gender']) and !empty($_POST['excel_gender']))
 {
  $query.= " AND gender = '{$_POST['excel_gender']}'";
 }
if (isset($_POST['excel_college']) and !empty($_POST['excel_college']))
 {
 $query.= " AND college = '{$_POST['excel_college']}' ";
 }
if (isset($_POST['excel_department']) and !empty($_POST['excel_department']))
 {
 $query.= " AND department = '{$_POST['excel_department']}' ";
 }

 if (isset($_POST['excel_nationality']) and !empty($_POST['excel_nationality']))
 {
 $query.= " AND nationality = '{$_POST['excel_nationality']}' ";
 }

if (isset($_POST['excel_finalstatus']) and !empty($_POST['excel_finalstatus']))
 {
  $query.= " AND finalstatus = '{$_POST['excel_finalstatus']}' ";
 }

if (isset($_POST['excel_fyear']) and !empty($_POST['excel_fyear']))
 {
  $query.= " AND fyear = '{$_POST['excel_fyear']}' ";
 }
 $result = mysqli_query($conn,$query);

 echo "<table border='1'>
 <tr>
 <th>الإسم</th><th>تاريخ وصول الملف</th><th>الكلية</th><th>القسم</th>   <th>العمر</th>
 <th>الجنسية</th><th>الجنس</th><th>الدرجة</th><th>نوع التعيين</th><th>السنة</th>
 <th>البكالوريوس<br>سنة التخرج</th><th>الماجستير<br>سنة التخرج</th>
 <th>الدكتوراه<br>سنة التخرج</th><th>الملاحظات</th><th>تاريخ اللجنة</th>
 </tr>";
 while($res = mysqli_fetch_assoc($result)){
  echo "<tr><td>".$res['name']."</td><td>".$res['DOA']."</td><td>".$res['college']."</td><td>".$res['department']."</td><td>".$res['DOB']."</td><td>".$res['nationality']."</td><td>".$res['gender']."</td><td>".$res['fwtype']."</td><td>".$res['finalstatus']."</td><td>".$res['fyear']."</td>";

 $query2 = "SELECT * FROM abc where ".$res['id']." = DId";
 $result1 = mysqli_query($conn,$query2);

if (mysqli_num_rows($result1) > 0) {
    while($row = mysqli_fetch_assoc($result1)) {
        echo "<td>".$row['DUName']."<br>".$row['DYEAR']."</td>";
    }
}
echo "</tr>";
}
echo "</table>";
exit();
?>

请在您的export_excel.php 中使用此代码,如果出现错误请告诉我

【讨论】:

  • 添加标题后,同样的事情。
  • 我需要建议。我卡住了
  • 请检查我的新答案
  • 我收到报告但没有任何数据。我会尽快回复您并告知您的状态。
  • 那是因为你的查询或连接
【解决方案2】:

我没有找到解决方案,所以我使用 java 脚本及其解析,但任何人都有解决此问题的方法,请告诉我。

function fnExcelReport()
 {
var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j=0;
tab = document.getElementById('data'); // id of table

for(j = 0 ; j < tab.rows.length ; j++) 
{     
    tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
    //tab_text=tab_text+"</tr>";
}

tab_text=tab_text+"</table>";
tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE "); 

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
{
    txtArea1.document.open("txt/html","replace");
    txtArea1.document.write(tab_text);
    txtArea1.document.close();
    txtArea1.focus(); 
    sa=txtArea1.document.execCommand("SaveAs",true,"Export.xls");
}  
else                 //other browser not tested on IE 11
    sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

return (sa);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-09
    • 2013-03-19
    • 2012-04-19
    • 2011-03-31
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多