将mysql结果数组导出为ex​​cel

时间:2015-07-11 17:17:19

标签: php mysql phpmyadmin export-to-excel

当我将此查询"SELECT name , domain FROM my_data ORDER by id"运行到phpmyadmin时,结果是这样的:

     name   domain  
    Bahamdan Group Holding Co.  salam.com
    ARINA RAMLEE    salam.net

这是我的代码:

 $con1 = mysqli_connect("localhost", "root", "", "test_pr");
         $sql2 = "SELECT name , domain FROM my_data ORDER by id";
         $result2 = mysqli_query($con1, $sql2);
         $rows = array();
         while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
             $rows[] = $row . PHP_EOL;
         }
         $nn = implode("", $rows);
         var_dump($rows);
         echo $nn . PHP_EOL;
         $file = fopen("export.csv", "w");
         file_put_contents("export.csv", $nn);
         fclose($file);

但它不起作用...... 我想把它放在一个文件中,就像在phpmyadmin中显示它一样。 我的意思是我希望将它放在一个excel文件中,该文件有两列,其中一列是名称,另一列是域,并且列下有行数据。 我尝试了这个,但是我不知道如何创建我想要的文件。

 $con1 = mysqli_connect("localhost", "root", "", "test_pr");
         $sql2 = "SELECT name , domain FROM my_data ORDER by id";
         $result2 = mysqli_query($con1, $sql2);
         $rows = array();
         while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
             $domains[] = $row['domain'] . PHP_EOL;
             $name[] = $row['name']. PHP_EOL;
         }

var_dump($domains);
var_dump($name);
         $nn = implode("", ??????);
         $file = fopen("export.csv", "w");
         file_put_contents("export.csv", $nn);
         fclose($file);

这是vardump

array (size=2)
  0 => string 'salam.com
' (length=11)
  1 => string 'salam.net
array (size=2)
  0 => string 'Bahamdan Group Holding Co.
' (length=28)
  1 => string 'ARINA RAMLEE

1 个答案:

答案 0 :(得分:0)

以下是使用用于获取数据的MySQL查询生成Excel工作表的示例代码。

//create query to select as data from your table
$select = "SELECT * FROM table_name";

//run mysql query and then count number of fields
$export = mysql_query ( $select ) 
       or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );

//create csv header row, to contain table headers 
//with database field names
for ( $i = 0; $i < $fields; $i++ ) {
    $header .= mysql_field_name( $export , $i ) . ",";
}

//this is where most of the work is done. 
//Loop through the query results, and create 
//a row for each
while( $row = mysql_fetch_row( $export ) ) {
    $line = '';
    //for each field in the row
    foreach( $row as $value ) {
        //if null, create blank field
        if ( ( !isset( $value ) ) || ( $value == "" ) ){
            $value = ",";
        }
        //else, assign field value to our data
        else {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . ",";
        }
        //add this field value to our row
        $line .= $value;
    }
    //trim whitespace from each row
    $data .= trim( $line ) . "\n";
}
//remove all carriage returns from the data
$data = str_replace( "\r" , "" , $data );


//create a file and send to browser for user to download
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$file_name.".csv");
print "$header\n$data";
exit;