PHPExcel generating real xls and xlsx files from database
===

I tried PHPExcel download package test 01. The example used object to specify which cell contains what, and produce an excel file.

Besides, I tried the following codes to generate an excel file from database:-

    	$file_type = "vnd.ms-excel";
    	$file_ending = "xls";
    
    
    //header info for browser: determines file type ('.doc' or '.xls')
    header("Content-Type: application/$file_type");
    header("Content-Disposition: attachment; filename=$file_name.$file_ending");
    header("Pragma: no-cache");
    header("Expires: 0");
    
    $xls_header = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=utf-8" />
    </head>
    <body>
    <table border="1" align="center">';
    
    $xls_footer = '</table>
    </body>
    </html>';
    print $xls_header;
    
    /*	Start of Formatting for  Excel	*/
    
    
    	/*	FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
    	//create title with timestamp:
    	if ($Use_Title == 1)
    	{
    		echo "<tr><td colspan='".mssql_num_fields($result)."'>";
    		echo("$title");
    		echo "</td></tr>";
    	}
    	//define separator (defines columns in excel & tabs in word)
    	//$sep = "\t"; //tabbed character
    
    		echo "<tr>";
    	//start of printing column names as names of MySQL fields
    	for ($i = 0; $i < mssql_num_fields($result); $i++)
    	{
    		echo "<td>";
    		echo mssql_field_name($result,$i);
    		echo "</td>";
    	}
    	print("</tr>");
    	//end of printing column names
    
    	//start while loop to get data
    	while($row = mssql_fetch_array($result))
    	{
    		$schema_insert .= "<tr>";
    		//set_time_limit(60); // HaRa
    		$schema_insert = "";
    		for($j=0; $j< mssql_num_fields($result);$j++)
    		{
    			$schema_insert .= "<td>";
    			if(!isset($row[$j]))
    				$schema_insert .= "NULL".$sep;
    			elseif ($row[$j] != "")
    				$schema_insert .= "$row[$j]".$sep;
    			else
    				$schema_insert .= "".$sep;
    			$schema_insert .= "</td>";
    		}
    		$schema_insert .= "<tr>";
    		$schema_insert = str_replace($sep."$", "", $schema_insert);
    		//following fix suggested by Josue (thanks, Josue!)
    		//this corrects output in excel when table fields contain \n or \r
    		//these two characters are now replaced with a space
    		$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
    		$schema_insert .= "\t";
    		print(trim($schema_insert));
    		print "\n";
    	}
    	print $xls_footer;

but excel program informs me that it is not a real excel file. if I wish to generate real excel files for xls and xlsx format with data from database, is there any good example?

Thanks!