簡體   English   中英

如何將選定的MySQL數據導出到Excel電子表格

[英]How to Export Selected MySQL Data to an Excel Spread Sheet

我目前正在網站的導出端工作,以使用戶能夠查看保存在MySQL數據庫上的數據。 目前,我有一個可行的解決方案,它將在查詢中選擇的所有數據傳遞到.CSV文件。

我遇到的問題是我希望能夠讓用戶選擇自己的數據,然后能夠將顯示的數據導出到電子表格而不是整個表格。

解決該問題的第一個想法是將執行的查詢存儲到變量中,然后在使用導出腳本時將其包括在內。

會推薦這種方法還是我走錯了路? 如果有人可以幫助我,我將非常感激。

另外,如果這個問題不合適,請說明原因。 所有反饋將對將來的帖子有用。

您需要使用phpexcel庫:

這個完整的例子我之前寫過

<?php
include("classes/PHPExcel.php");
    set_time_limit(0);

$sql="Select * from datbase";

// Execute the database query
$result = mysql_query($sql) or die(mysql_error());

// Instantiate a new PHPExcel object
$objPHPExcel = new PHPExcel(); 
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0); 
// Initialise the Excel row number
$rowCount = 1; 
// Iterate through each result from the SQL query in turn
// We fetch each database result row into $row in turn
while($row = mysql_fetch_array($result)){ 
    // Set cell An to the "name" column from the database (assuming you have a column called name)
    //    where n is the Excel row number (ie cell A1 in the first row)

    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['user_id']); 
    // Set cell Bn to the "age" column from the database (assuming you have a column called age)
    //    where n is the Excel row number (ie cell A1 in the first row)
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['email'])); 

    $objPHPExcel->getActiveSheet()->SetCellValue('c'.$rowCount,$row['email']); 

     $objPHPExcel->getActiveSheet()->SetCellValue('d'.$rowCount,$row['email']); 
     $objPHPExcel->getActiveSheet()->SetCellValue('e'.$rowCount,$row['email']); 
      $objPHPExcel->getActiveSheet()->SetCellValue('f'.$rowCount,$row['email']); 
    // Increment the Excel row counter
    $rowCount++;      

} 

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
// Write the Excel file to filename some_excel_file.xlsx in the current directory
$objWriter->save('all_data.xlsx'); 



?>

有關更多信息,請參見廣告示例: https : //phpexcel.codeplex.com/wikipage?title=Examples

暫無
暫無

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

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