通过php查询输出到xls不起作用

时间:2016-01-29 07:52:44

标签: php mysql xslt

我们创建了一个页面,可以使用普通&#34; select * from <table_name>;&#34;查询单个表格。并以 XSL 格式提供输出。 代码在我们的 Dev 环境中正常工作,但在 Prod 中失败。

&#13;
&#13;
<?php require_once('Connections/cnca.php');
//create query to select as data from your table
$select = "SELECT * FROM fevisitdetails ";
        
//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 = "\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;
	}
	//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/octet-stream");
header("Content-Disposition: attachment; filename=fedetails.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";exit;
?>
&#13;
&#13;
&#13;

您认为我需要更改 Mysql / Php 的任何参数才能实现此目的吗?

请帮忙......

1 个答案:

答案 0 :(得分:0)

作为生产中似乎失败的原始代码的替代品,您可以使用以下内容。它使用mysqli连接&amp;函数 - (因为mysql_*函数系列已被弃用,建议不要继续使用它们)并且比mysql函数有几个优点〜例如fetch_fields()

不是生成和连接复杂的字符串,而是使用fputcsv - 它是专为工作而设计的,因此应该可以避免许多麻烦。

我无法保证这会解决您遇到的问题,但它在测试中对我有用,并且可以非常快速地生成5000行csv文件。

ob_clean();

/* edit your db connection details */
$dbhost =   'xxx';
$dbuser =   'xxx'; 
$dbpwd  =   'xxx'; 
$dbname =   'xxx';

/* safer & simpler with mysqli */
$conn   =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );        


$sql="select * from `fevisitdetails`;";
$res=$conn->query( $sql );

if( $res ){

    $headers=array();

    /* create a temporary file */
    $tmp = tempnam( sys_get_temp_dir(), 'csv' );
    $out = fopen( $tmp, 'w');

    /* Fetch as array of objects - columns */
    $fields = $res->fetch_fields();

    /* Get the column / field names */
    foreach( $fields as $field ) $headers[]=$field->name;

    /* Add column headers */
    fputcsv( $out, $headers );

    /* Add each row ( array ) to the csv */
    while( $row=$res->fetch_array( MYSQLI_ASSOC ) ){
        fputcsv( $out, $row );
    }
    /* close the file handle */
    fclose( $out );

    /* set headers and send csv data ~ works better with `.csv` extension */
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=fedetails.csv");
    header("Pragma: no-cache");
    header("Expires: 0");

    /* echo data from csv */
    echo file_get_contents( $tmp );
    @unlink( $tmp );                

}
/* close db conn */
$conn->close();
exit();