为什么我在此CSV导出的顶部有一个空行,如何删除它?

时间:2017-12-13 14:48:28

标签: php csv

 $conn = mysql_connect($entr,$user,$cred);          //Set db connection
 mysql_select_db("webshops",$conn);
 $result = mysql_query('CALL storedproc');    


 $num_column = mysql_num_fields($result);                 //Get number of columns, for each column header get name and give them individual columns.    
 for($i=0;$i<$num_column;$i++) {
     $csv_header .= '"' . mysql_field_name($result,$i) . '";';
 }  
 $csv_header .= "\n";

 $csv_row ='';                               
 while($row = mysql_fetch_row($result)) {
     for($i=0;$i<$num_column;$i++) {
         $csv_row .= '"' . $row[$i] . '";';
     }
     $csv_row .= "\n";
 }
 ob_end_clean();

 header('Content-type: application/csv');
 header('Content-Disposition: attachment; filename=OrderExport.csv');
 echo $csv_header . $csv_row;
 exit;

在Excel中下载并打开此CSV后,第一行始终为空白。标题从第2行开始。如何防止这种情况发生并且头文件首先开始?非常感谢。

2 个答案:

答案 0 :(得分:2)

您最好使用fputcsv http://php.net/manual/en/function.fputcsv.php。我已经更改了您的代码,但我无法对其进行测试,因此可能无法完美运行,但希望它会给您一个想法。此外,正如其他人在评论中所说,你不应该使用mysql,因为该API已经过时了。您应该使用mysqli代替。

 $conn = mysql_connect($entr,$user,$cred);          
 mysql_select_db("webshops",$conn);
 $result = mysql_query('CALL storedproc');    
 $num_column = mysql_num_fields($result);                    

 $fp = fopen('mycsv.csv', 'w');
 $csv_header = array();

 for($i=0;$i<$num_column;$i++) {
     array_push($csv_header, mysql_field_name($result,$i));
 }  

 fputcsv($fp, $csv_header);

 while($row = mysql_fetch_row($result)) {
     $csv_row = array();          
     for($i=0;$i<$num_column;$i++) {
         array_push($csv_row, $row[i]);
     }
     fputcsv($fp, $csv_row);
 }
 ob_end_clean();

 header('Content-type: application/csv');
 header('Content-Disposition: attachment; filename=OrderExport.csv');
 fclose($fp);
 exit;

答案 1 :(得分:0)

要以更可靠和正确的方式从存储过程输出结果,请使用mysqliPDO来处理数据库操作,使用内置fputcsv来格式化数据用于csv输出。

<?php

    /* File will be called this when it is downloaded */        
    $filename = 'mycsvfile.csv';

    /* create db connection using mysqli ( or PDO ) but not mysql */    
    $dbhost =   'localhost';
    $dbuser =   'root'; 
    $dbpwd  =   'xxx'; 
    $dbname =   'xxx';
    $conn   =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );

    /* create sql query */
    $sql='call `storedproc`();';

    /* run the query */
    $result=$conn->query( $sql );
    if( $result ){

        /* store column names in this array - used to create csv headers */
        $cols=array();

        /* iterate through column names and add to array $cols */
        array_walk( $result->fetch_fields(), function( $value, $key, $cols ){
            $cols[]=$value->name;
        },&$cols );

        /* create an output stream to write csv to */
        $stream=fopen( 'php://output', 'w' );
        /* add column headers */
        fputcsv( $stream, $cols );

        /* iterate through the recordset returned by stored procedure */
        while( $rs=$result->fetch_assoc() ){
            fputcsv( $stream, array_values( $rs ) );
        }
        /* capture stream contents */
        $data=stream_get_contents( $stream );

        /* close the output stream */
        fclose( $stream );

        /* send headers and write content to browser */
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename=' . $filename );
        exit( $data );
    }
?>