All fields are showing in one column when exportin

2019-09-06 09:23发布

I have written a simple php script for download excel file which contain some MySQL data fetching by a MySQL query. Data is coming correctly but when I am printing that result in excel file, all fields are coming in the first column of excel file, which is not correct. I want single field in single column. what I am missing in the code. Please If anyone have any idea how to achieve that. Here is my code -

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;

Here is the screen shot of excel file which I am getting after running this script-enter image description here

3条回答
姐就是有狂的资本
2楼-- · 2019-09-06 10:06

Replace following part of your code:

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

With :

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

and check it works for me

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-06 10:06

What about create a .csv file? You can use the function fputcsv() from PHP to help you. A comma-separated values (CSV) (or character-separated values) file stores tabular data (numbers and text) in plain-text form. It is relatively simple to use and works great with Excel.

As an example of code, you could do this:

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

References:

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

The Comma Separated Value (CSV) File Format - http://creativyst.com/Doc/Articles/CSV/CSV01.htm

查看更多
ゆ 、 Hurt°
4楼-- · 2019-09-06 10:27

Try with this change :

header( "Content-disposition: filename=".$file_name.".csv");
查看更多
登录 后发表回答