简体   繁体   English

PHP MySQL将数据导出到Excel

[英]PHP MySQL Export data to excel

I have the following code to export the data from the table that the user choose, but for some reason it's not downloading the file.. 我有以下代码从用户选择的表中导出数据,但是由于某种原因,它没有下载文件。
here's my export.php: 这是我的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;
 }
}
?>

And on my index.php I have this: 在我的index.php上我有这个:

<form method="post" action="export.php">
     <input type="submit" name="export" class="btn btn-success" value="Export" />
    </form>

It doesn't give me any error at all, just stay where it was and don't download anything 它根本不会给我任何错误,只是保持原样,不要下载任何东西

Try This code 试试这段代码

<?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();
?>

Solved. 解决了。 :) :)

<?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;
 }

?>

Try header code into top of the file and remove from bottom. 尝试将标题代码放入文件顶部并从底部删除。

// 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM