Sorgu Sonucunu Excel’e Aktarmak

<?PHP
error_reporting(4);
class ExportToExcel
{
	function exportWithPage($php_page,$excel_file_name)
	{
		$this->setHeader($excel_file_name);
		require_once "$php_page";
	}
	function setHeader($excel_file_name)
	{
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=$excel_file_name");
		header("Pragma: no-cache");
		header("Expires: 0");
	}
	function exportWithQuery($qry,$excel_file_name,$conn)//to export with query
	{
		$tmprst=mysql_query($qry,$conn);
		$header="<center><table border=1px><th>Tablo Dökümü</th>";
		$num_field=mysql_num_fields($tmprst);
		while($row=mysql_fetch_array($tmprst,MYSQL_BOTH))
		{
			$body.="<tr>";
			for($i=0;$i<$num_field;$i++)
			{
				$body.="<td>".$row[$i]."</td>";
			}
			$body.="</tr>";	
		}
		$this->setHeader($excel_file_name);
		echo $header.$body."</table";
	}
}
$conn=mysql_connect('localhost','root','')or die('bağlanmadı');
mysql_select_db('test');


//ÖRNEK
$exp=new ExportToExcel();

//$exp->exportWithPage("export_file.php","test.xls");

$qry="select * from qa_users";
$exp->exportWithQuery($qry,"test.xls",$conn);

?>