使用php将mysql数据导出到excel时,所有字段都显示在一列中

时间:2014-12-02 12:19:59

标签: php mysql excel export-to-excel

我为下载excel文件编写了一个简单的php脚本,其中包含一些MySQL查询提取的MySQL数据。数据正确,但是当我在excel文件中打印结果时,所有字段都出现在excel文件的第一列中,这是不正确的。我想在单列中使用单个字段。我在代码中遗漏了什么。如果有人知道如何实现这一点。这是我的代码 -

include('db_connect.php');
$select = "select `dp_id`,`client_id`,`invester_name`,`pan_no` from 
(SELECT `dp_id` , `client_id` , `invester_name` , `pan_no`
FROM `app_form` union all
SELECT `dp_id`,`client_id`,`first_holder_name`,`first_holder_pan` FROM `response_record`)
tbl order by `dp_id`,`client_id`";

//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 );

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

以下是运行此脚本后我得到的excel文件的屏幕截图 - enter image description here

3 个答案:

答案 0 :(得分:1)

替换以下部分代码:

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;
}

使用:

foreach( $row as $value ) {
    //if null, create blank field
    if ( ( !isset( $value ) ) || ( $value == "" ) ){
        $value = "\t";
    }
    //else, assign field value to our data
    else {
        $value = str_replace( '"' , '""' , $value );
        $value = '"' . $value . '"' . "\t";
    }
    //add this field value to our row
    $line .= $value;
}

并检查它是否适合我

答案 1 :(得分:0)

尝试进行此更改:

header( "Content-disposition: filename=".$file_name.".csv");

答案 2 :(得分:0)

创建.csv文件怎么样?您可以使用PHP中的函数fputcsv()来帮助您。逗号分隔值(CSV)(或字符分隔值)文件以纯文本形式存储表格数据(数字和文本)。它使用起来相对简单,适用于Excel。

作为代码示例,您可以这样做:

<?php

// The data that comes from your MySQL database
$list = array (
    array('ID', 'NAME', 'SALARY'),
    array('123', 'John Doe', '10000'),
    array('456', 'Sara Parker', '12000')
);

$fp = fopen('file.csv', 'w'); // Your .csv file

// Inserting data in your file using the pointer created above
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

参考文献:

fputcsv() - http://php.net/manual/en/function.fputcsv.php

逗号分隔值(CSV)文件格式 - http://creativyst.com/Doc/Articles/CSV/CSV01.htm