从mysql表导入Excel

<?php 
require_once("db_connect.php");
$arr = $_GET['arr'];

$selected_header = implode(', ', (array)$arr);

$sql = "SELECT $selected_header FROM release_ ORDER BY id DESC";

header('Content-Type: application/vnd.ms-excel');   
header('Content-Disposition: attachment; filename=CNC-'.date('Ymd').'.xls');
header('Pragma: no-cache');
header('Expires: 0');

$output = '';
$output .= '<table><tr>';

$header_arr = explode(',', $arr);
$len = count($header_arr);


for($a=0; $a<$len; $a++){
    $output .= "<th style='width: 100px; text-align: center;'>" . $header_arr[$a] . "</th>";
}
$output .= '</tr>';

$res = mysqli_query($conn, $sql);

/*====this ruins everything=====*/
while($row = $res->fetch_assoc()){
    $output = '<tr align="center">';
    for($a=0; $a<$len; $a++){
        $output .= "<td>" . $header_arr[$a] . "</td>";
    }

    echo "</tr>\t
";
}
/*===============================*/

$output .= '</table>';
echo $output;

?>

I am able to get and display the header in the excel file but the content is not displaying at all. I get the whole string of row tag displayed. How am I able to display the data from the database to excel file. Please help.

I have done in my project(Cakephp) you can use like this own way :-

 public function ExportToExcel(){
    $users_id = $this->Session->read('selected_user_id');
    $this->loadModel('User');
    $conditions= array('User.usertype !='=>1);
    if(!empty($users_id)){
        $ids=explode(",",$users_id);
        $conditions = array(
            'User.id'=>$ids
            );
    }
    $users = $this->User->find('all',array(
        'conditions'=>$conditions,
        'order'=>array('User.id Desc') 
        )
    );
    ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large
    $filename = "Users" . date("Y.m.d") . ".xls";
    header('Content-type: application/ms-excel');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    $i = 1;
    $row="";
    $row_header="S.No."."\t"."Name "."\t"."Email"."\t"."Mobile"."\t"."Address"."\t"."Status"."
";
    if(!empty($users)){
        foreach($users as $user){
            if($user['User']['status']==1){
                $status = "Active";
            }
            else{
                $status = "Inactive";
            }

            $row .= $i++."\t".$user['User']['firstname'].' '.$user['User']['lastname']
            ."\t".$user['User']['email']
            ."\t".$user['User']['phone_number']
            ."\t".$user['User']['address']
            ."\t".$status
            ."
";
        }
    }
    $this->Session->delete('selected_user_id');
    echo($row_header);
    echo($row);
    die;
}