0

听起来很简单,但我不知道从哪里开始。

我的网站有一系列可供用户选择的选项。从该选择中,构建一个查询,并从数据库中提取数据。然后以表格格式返回。

此时,数据显示出来,用户可以单击按钮将记录导出到 Excel 工作表中。

这是查询:

 <?php 
   function displayrecords(){
   $place = $_GET['place'];
   $time = $_GET['time'];
   $name = $_GET['name'];
   $sql_json = "";
   $where = "";
   if($place != ""){
     $where = " `place` = '".mysql_real_escape_string($place)."'";
   }
   if($time != ""){
     if( $where != "" ) $where .= " AND ";
     $where .= " `time` = '".mysql_real_escape_string($time)."'";
   }
   If($name != ""){
     if( $where != "" ) $where .= " AND ";
     $where .= " `name` = '".mysql_real_escape_string($name)."'";
   }

   if ($where != "") $sql_json = "SELECT * FROM `database`" . " WHERE " . $where . ";";
   $QueryResult = @mysql_query($sql_json) or die ('<div>Use filters to search</div>');
   $resnum = mysql_num_rows($QueryResult);
   if ($resnum == 0){
     echo "<div>Your search returned no results</div";
   }
   else {
     echo "<table>\n";
     echo "<tr><th>Place</th>" . "<th>Time</th>" . "<th>Name</th></tr>\n";
     while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE){
     echo "<tr><td>{$Row[place]}</td>";
     echo "<td>{$Row[time]}</td>";
     echo "<td>{$Row[name]}</td></tr>";
   };
   echo "</table>\n";
   }
   }
   ?>

此时我可以在 html 中调用该函数:

 <?php displayrecords(); ?>

然后这是我用来导出的按钮:

 <input class="btn btn-success btn-small" type="submit" name="get_report" value="Get Report" /> 

这就是我卡住的地方。我知道我可能不得不使用 javascript 或其他东西。请给我一些见解。无需关闭我的代码。我使用的是常规 mysql,而不是 mysqli 或 pdo。

请告诉我下一步我需要做什么才能使按钮工作,以便它导出到 Excel 工作表。我会很感激你的帮助。

谢谢你。

4

2 回答 2

0

我建议您使用PHPExcel库将数据转换为 excel。它是免费的,而且非常易于使用。在您的情况下,您可以执行以下操作:

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);

$excelRow = 1; // just an integer to track where in the sheet we are
while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE)
{
  // This only puts the data into the spread sheet and does not print it

  $objPHPExcel->getActiveSheet()->SetCellValue('A' . $excelRow, $Row['place']);
  $objPHPExcel->getActiveSheet()->SetCellValue('B' . $excelRow, $Row['name']);
  $objPHPExcel->getActiveSheet()->SetCellValue('C' . $excelRow, $Row['time']);

  $excelRow++;
}

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('myfile.xlsx')); // or stream the file contents to output buffer so the user downloads it.
于 2013-10-01T20:02:36.687 回答
0

只需检查按钮是否被按下并相应地返回 html 或 excel 数据。

   if ($resnum == 0){
     echo "<div>Your search returned no results</div";
   }
   else {
     if (isset($_POST['get_report']){
         // Use phpExcel class or just echo CSV formatted text with headers telling the browser its a CSV file
     }else{
       echo "<table>\n";
       echo "<tr><th>Place</th>" . "<th>Time</th>" . "<th>Name</th></tr>\n";
       while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE){
         echo "<tr><td>{$Row[place]}</td>";
         echo "<td>{$Row[time]}</td>";
         echo "<td>{$Row[name]}</td></tr>";
       }
   }

PHPExcel

于 2013-10-01T19:42:39.360 回答