PHP MySQL将数据导出到excel

PHP MySQL将数据导出到excel,php,Php,我有以下代码可以从用户选择的表中导出数据,但由于某些原因,它没有下载文件。。 下面是我的export.php: <?php //export.php session_start(); $DataDeConsulta = $_SESSION['DataDeConsulta']; //export.php $connect = mysqli_connect("localhost", "user", "pw", "filecleaner"); $output = ''; if(iss

我有以下代码可以从用户选择的表中导出数据,但由于某些原因,它没有下载文件。。 下面是我的export.php:

<?php  
//export.php  
session_start();
$DataDeConsulta = $_SESSION['DataDeConsulta'];
//export.php  
$connect = mysqli_connect("localhost", "user", "pw", "filecleaner");
$output = '';
if(isset($_POST["export"]))
{
 $query = "SELECT * FROM filecleaner.`Opened_". $DataDeConsulta ."`";
 $result = mysqli_query($connect, $query);
 if(mysqli_num_rows($result) > 0)
 {
  $output .= '
   <table class="table" bordered="1">  
                    <tr>  
                         <th>Emails</th>  
                    </tr>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '
    <tr>  
                         <td>'.$row["Emails"].'</td>  
                    </tr>
   ';
  }
  $output .= '</table>';
  header('Content-Type: application/xls');
  header('Content-Disposition: attachment; filename=download.xls');
  echo $output;
 }
}
?>
在我的index.php上,我有:

<form method="post" action="export.php">
     <input type="submit" name="export" class="btn btn-success" value="Export" />
    </form>
它根本没有给我任何错误,只是呆在原处,不要下载任何东西。

试试这段代码

<?php  
ob_start();

---
---- 
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=members".date('d-m-Y').".csv");
echo $output;
ob_end_flush();
?>
已解决:

<?php  
//export.php  

session_start();
$DataDeConsulta = $_SESSION['DataDeConsulta'];

//export.php  
$connect = mysqli_connect("localhost", "user", "pw", "filecleaner");
$output = '';

 $query = "SELECT * FROM filecleaner.`Opened_". $DataDeConsulta ."`";
 $result = mysqli_query($connect, $query);
 if(mysqli_num_rows($result) > 0)
 {
  $output .= '
            Emails
  ';
  while($row = mysqli_fetch_array($result))
  {
    $output .= ''.$row["Emails"].'
    ';
  }
  header('Content-Type: application/csv');
  header("Content-Type: application/force-download");
  header('Content-Disposition: attachment; filename=ics2019.csv');
  //
  echo $output;
 }

?>

尝试将头代码插入文件顶部并从底部删除

// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");
// Defines the name of the export file "download.xls"
header("Content-Disposition: attachment; filename=download.xls");

它不会显示任何错误消息,因为它在另一个文件中。不,我所有的错误都指向一个文件。为什么第二个文件是if mysqli_num_rows而不是mysqli_fetch_array?注释掉标题,然后看看会发生什么。或者在浏览器开发工具的“网络”面板中检查请求。发生的情况与此相同:/