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

我已经写了一个简单的PHP脚本下载excel文件,其中包含一些MySQL查询获取MySQL数据。 数据是正确的,但是当我在excel文件中打印结果时,所有字段都在excel文件的第一列中出现,这是不正确的。 我想单栏中的单个字段。 我在代码中缺less的东西。 请如果有人有任何想法如何实现这一点。 这是我的代码 –

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文件的屏幕截图 – 在这里输入图像说明

replace以下部分代码:

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

并检查它适用于我

尝试这个改变:

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

怎么样创build一个.csv文件? 你可以使用PHP的函数fputcsv()来帮助你。 逗号分隔值(CSV)(或字符分隔值)文件以纯文本forms存储表格数据(数字和文本)。 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