使用php将mysql数据导出到excel时,所有字段都显示在一列中
我为下载excel文件编写了一个简单的php脚本,其中包含一些通过MySQL查询获取的MySQL数据。数据正确,但当我打印excel文件时,所有字段都出现在excel文件的第一列,这是不正确的。我希望在单个列中包含单个字段。我在代码中缺少了什么。如果有人知道如何做到这一点,请告诉我。这是我的密码-使用php将mysql数据导出到excel时,所有字段都显示在一列中,php,mysql,excel,export-to-excel,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 (SEL
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文件的屏幕截图-尝试此更改:
header( "Content-disposition: filename=".$file_name.".csv");
创建一个.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-
逗号分隔值CSV文件格式-
替换代码的以下部分:
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;
}
并检查它是否适用于我使用;作为分离字符。我不知道这是否适用于xls文件,但也可以尝试csv文件。1您创建的是csv文件,而不是Excel BIFF格式的.xls文件。2使用PHP的内置函数创建CSV文件3您需要使用的分隔符在MS Excel中是特定于区域设置的,请尝试使用;而不是,@S.Pols-OP正在生成csv文件,而不是xls文件。。。。扩展名为.xls并不意味着文件格式自动转换为BIFFso。如何生成excel文件。请建议我更改您可以使用的扩展名,以便您的代码真正做的是用制表符分隔符替换逗号分隔符。。。。你为什么不这么说?还是一样的结果伙计…是的,我根据你做了一些改变。现在它正在工作\t正在工作,谢谢兄弟。我知道这是一个非常简单的方法,但我真的需要直接从浏览器的excel文件。