簡體   English   中英

PHP MySQL將數據導出到Excel

[英]PHP MySQL Export data to excel

我有以下代碼從用戶選擇的表中導出數據,但是由於某種原因,它沒有下載文件。
這是我的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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM