php - 将 MySQL 数据导出到 MS Excel 在本地运行良好,但在服务器上运行不佳

标签 php html mysql excel

<title>Orders Export</title>

<body>


<?php 
 header('Content-Type: application/xls');
  header('Content-Disposition: attachment; filename=download.xls');

$con = mysqli_connect('localhost','suresafe_admin','iwant$100','suresafe_suresafety');



    $query = "select * from `orders_approval` where `approve`='1' and `company_name`='GAVL-F111'";

    $result = mysqli_query($con,$query);
$output = '';
if(mysqli_num_rows($result) > 0)
 {
  $output .= '?>
   <table class="table" bordered="1">  
                    <tr>  
                         <th>order_no</th>  
                         <th>order_date</th>  
                         <th>order_name</th> 
                         <th>total_amount</th> 

                    </tr><?php
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '
    <tr>  
                         <td>'.$row["order_no"].'</td>  
                         <td>'.$row["order_date"].'</td>  
                         <td>'.$row["order_name"].'</td>  
                         <td>'.$row["total_amount"].'</td>  

                    </tr>
   ';
  }
  $output .= '</table>';


 }?>


</body>
</html>

代码很简单。在本地工作得很好。但是当我在服务器上的网站中使用它时。它仅显示包含数据的表。它不会在 Excel 中导出该数据。

order_no    order_date  order_name  total_amount
100000705   2017-05-07  MR. PRADEEP Y   113500
100000708   2017-05-11  MR. A SRINIVASA RAO     5448
100000725   2017-05-30  MR. A SRINIVASA RAO     77180 

这是我点击导出链接时可以看到的结果。

在本地服务器中,可以轻松导出。

最佳答案

如果你有configured your server properly ,您会看到警告。数据输出后无法发送 header 。此外,您发送的数据不是 application/xls(这不是有效的 MIME 类型),而是 text/html。您还将“?>”和“

但是,我建议您使用 CSV 来输出数据:

<?php
$con = mysqli_connect("localhost", "suresafe_admin", "iwant$100", "suresafe_suresafety");
$query = "SELECT order_no, order_date, order_name, total_amount from orders_approval WHERE approve = 1 AND company_name = 'GAVL-F111'";
$result = mysqli_query($con, $query);
$output = "";
if(mysqli_num_rows($result) > 0) {
    $output .= "order_no,order_date,order_name,total_amount\n";
    while($row = mysqli_fetch_assoc($result)) {
        $output .= "$row[order_no],$row[order_date],$row[order_name],$row[total_amount]\n";
    }
}
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=download.csv");
echo $output;

关于php - 将 MySQL 数据导出到 MS Excel 在本地运行良好,但在服务器上运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44454403/

相关文章:

php - Zend_Db_Table Select missing data,直接PHP查询ok

html - CSS 箭头 : How can I center a text?

mysql - 使用子查询时无法选择id

javascript - 如何只打印首页

html - 使用 iTextSharp 使用 HTML 格式的文本填充 PDF 模板 acrofield

mysql - 打印多于 1 行,Codeigniter

php - 需要第二只眼睛来调试查询

php - 选择时弹出音频文件php

php - Symfony 更新模型架构

php - 如何将输入类型文件(即图像)目录作为 session 传递