PHP没有以正确的格式导出MySQL数据库

时间:2016-08-11 23:01:44

标签: php html mysql excel

因此我无法将数据从MySQL导出到excel

    $output = '';  
 if(isset($_POST["export_excel"]))  
 {  
      $sql = "SELECT * FROM Logs ORDER BY item";  
      $result = mysqli_query($connect, $sql);  
      if(mysqli_num_rows($result) > 0)  
      {  
           $output .= '  
                <table class="table bordered="1">  
                     <tr>  
                          <th>Sort</th>  
                          <th>Unit Size</th>  
                          <th>Quantity</th>  
                          <th>Price per Unit</th>  
                          <th>Time</th>  
                     </tr>  
           ';  
           while($row = mysqli_fetch_array($result))  
           {  
                $output .= '  
                     <tr>  
                          <td>'.$row["Sort"].'</td>  
                          <td>'.$row["Unit Size"].'</td>  
                          <td>'.$row["Quantity"].'</td>  
                          <td>'.$row["Price per Unit"].'</td>  
                          <td>'.$row["Time"].'</td>  
                     </tr>  
                ';  
           }  
           $output .= '</table>';  
           header("Content-Type: application/xls");   
           header("Content-Disposition: attachment; filename=download.xls");  
           echo $output;  
      }  
 } 

所以当我点击index.php页面上的导出按钮时,它会以这种格式输出数据:Google Sheets Link to the excel file

<table class="table bordered="1">   
<tr>    
<th>Sort</th>   
<th>Unit Size</th>  
<th>Quantity</th>   
<th>Price per Unit</th> 
<th>Time</th>   
</tr>   

<tr>    
<td></td>   
<td>45</td> 
<td>0</td>  
<td>0</td>  
<td>2016-08-11 16:53:12</td>    
</tr>   

<tr>    
<td></td>   
<td>6</td>  
<td>0</td>  
<td>0</td>  
<td>2016-08-11 16:53:12</td>    
</tr>   
</table>    

所以它输出正确的数据,但只是关闭格式化,这是它在index.php页面上的样子: What the excel file should look like 如果有人能告诉我我做错了那就太棒了! 提前谢谢!

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望将数据导出到Excel中。在这种情况下,只更改标题将不起作用,因为excel不是关于表标签,你需要的是一个库,用于将内容写入excel格式文件,然后使用户下载相同。看看phpExcel

您还可以在示例here中看到如何使用相同的方式在Excel中正确插入数据。

答案 1 :(得分:0)

也许有点作为csv输出作弊而不是proper xls,但它或多或少会起作用。

<?php

    if( isset( $_POST["export_excel"] ) ) {

        ob_clean();

        $sql = 'select * from logs order by item';
        $result = mysqli_query( $connect, $sql );  
        if( mysqli_num_rows( $result ) > 0){


            $delimiter=',';
            $enclosure='"';

            /* rather than create an actual file, use an output buffer and write to that */
            $output=fopen('php://output','w+');

            /* add column headers */
            $headers=array( 'Unit Size', 'Quantity', 'Price_per_Unit', 'Time' );

            /* write the headers to the output stream */
            fputcsv( $output,$headers, $delimiter, $enclosure );

            /* loop through recordset and add that to the stream */
            while( $row = mysqli_fetch_array( $result ) ) {
                fputcsv( $output, $row, $delimiter, $enclosure );
            }

            fclose( $output );

            /* set the headers accordingly */
            header('Content-Type: application/csv');
            header('Content-Disposition: attachment; filename=download.csv');

            /* send the new content */
            ob_flush();

        }
    }

?>