Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is my code to export to excel, but it display the data list and not to export in excel

PHP
function ExportToExcel($requests){		
	//print_r($requests);//exit;
		extract($requests);		
		 
		$query = "SELECT * FROM ".$tname." P WHERE 1";
		$cond_search = '';
		$Querycond = '';
		if(isset($dwnld) && ($dwnld == "dwnld_all")){
			$Querycond    .= " AND  1 ";
		}
		
		if(isset($dwnld) && ($dwnld == "new_submission")){
			$Querycond    .= " AND apply_on >= CURDATE()";
		}
		
		if(isset($dwnld) && ($dwnld == "all_submission")){
			$Querycond    .= " AND  3";
		}

		$query .=  $Querycond;
	
		$query1 = $query . " ORDER BY P.apply_on desc";
		
		$setRec = mysql_query($query1);
		  
		$developer_records = array();
		while( $rows = mysql_fetch_assoc($setRec) ) {
		$developer_records[] = $rows;
		} 	
		$filename = "export_".date('Ymd') . ".xls";
		header("Content-type: application/octet-stream");
		header("Content-Type: application/vnd.ms-excel; name='excel'");
		header("Content-Disposition: attachment; filename='".$filename."'");
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Cache-Control: max-age=0');

		$show_coloumn = false;
		if(!empty($developer_records)) {
		foreach($developer_records as $record) {
		if(!$show_coloumn) {
		// display field/column names in first row
		echo implode("\t", array_keys($record)) . "\n";
		$show_coloumn = true;
		}
		echo implode("\t", array_values($record)) . "\n";
		}
		}
		
		exit;		
		return true;
	}


What I have tried:

please help me, how to solve it,i have did in ajax call to php file then that php file to another php file
Posted
Updated 21-Jan-18 18:31pm
v2
Comments
Richard Deeming 19-Jan-18 10:52am    
Why are you sending three different Content-Type headers?

i am writing this
SELECT * FROM tname P WHERE 1
because of the P is used later as alias name like
$query1 = $query . " ORDER BY P.apply_on desc";
.
 
Share this answer
 
Good code always checks if function calls fail. To do that, inspect the return values and show error messages upon failures.

If you do that, you will be informed that your query fails because you are passing an invalid SQL command:
SQL
SELECT * FROM tname P WHERE 1
A valid command would be something like
SQL
SELECT * FROM tname WHERE P=1
Similar for the appending of " AND 1 " which is also not valid.

Finally, you are not creating an Excel file. You are creating a TAB separated text file which is recognised by Excel when importing. But it is not an Excel file and should be therefore not announced as such.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900