是否可以将搜索结果导出到Excel文档中?

时间:2018-03-27 01:50:26

标签: php mysql

我有一个功能正常的搜索栏和一个正常运行的excel编写器,但我不知道我将用什么查询从已经进行的搜索中导出结果。这可能吗?

我为搜索栏获得的代码是:

   <?php

    include ('database_conn.php');

    $output = '';

    if(isset($_POST['search'])) {
      $search = $_POST['search'];
      $search = preg_replace("#[^0-9a-z]i#","", $search);

      $query = mysqli_query($conn, "SELECT * FROM attendance WHERE stud_id LIKE '%$search%'") or die ("Could not search");
      $count = mysqli_num_rows($query);

      if($count == 0){
        $output = "There was no search results!";

      }else{

        while ($row = mysqli_fetch_array($query)) {


          $stud_id = $row ['stud_id'];
          $module = $row ['module'];
          $attendance_status = $row ['attendance_status'];

          $output .='<div> '.$stud_id.'    '.$module.'     '.$attendance_status.'</div>';
        }

      }
    }

    ?>

然后我对excel编写器的代码是:(数据库连接已经打开)       

      $output = '';
      if(isset($_POST["export"]))
   {

   $result=mysqli_query($conn, "SELECT * FROM attendance  ");
   if(mysqli_num_rows($result) > 0)
   {
    $output .= '
     <table class="table" bordered="1">  
                      <tr>  
                           <th>Name</th>  
                           <th>Module</th>  
                           <th>Status</th>
                           <th>Date</th>   
                      </tr>
    ';
    while($row = mysqli_fetch_array($result))
    {
     $output .= '
      <tr>  
                           <td>'.$row["stud_id"].'</td>  
                           <td>'.$row["module"].'</td>  
                           <td>'.$row["attendance_status"].'</td>  
                           <td>'.$row["date"].'</td>  
                      </tr>
     ';
    }
    $output .= '</table>';
    header('Content-Type: application/xls');
    header('Content-Disposition: attachment; filename=download.xls');
    echo $output;
   }
  }
  ?>

我现在知道我的导出按钮只会从考勤表中导出所有内容,我不确定使用哪个查询来导出搜索结果。

1 个答案:

答案 0 :(得分:0)

我推荐使用PhpSpreadsheet(PHPExcel的后继者)而不是自定义类。它允许您添加格式,字体样式,另存为XLSX等。

function export_excel_csv() {
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db("database",$conn);
    $rec = mysql_query("SELECT * FROM table") or die (mysql_error());        
    $num_fields = mysql_num_fields($rec);        
    for($i = 0; $i < $num_fields; $i++ )
        $header .= mysql_field_name($rec,$i)."\\t";        
    while($row = mysql_fetch_row($rec)) {
        $line = '';
        foreach($row as $value)  {                   
            if((!isset($value)) || ($value == "")) {
                $value = "\\t";
            }
            else {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\\t";
            }
            $line .= $value;
        }
        $data .= trim( $line ) . "\\n";
    }        
    $data = str_replace("\\r" , "" , $data);        
    if ($data == "")
        $data = "\\n No Record Found!\n";                        

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=reports.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\\n$data";
}

否则创建一个CSV字符串,设置内容标题以写入XLS文件,Excel将正确读取它。 E.g.

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');    

关于标题类型的问题,请检查此post并尝试:

apt-get purge openssh-client
相关问题