Excel Dosyası Oluşturmak

<?php
header("Content-type:application/vnd.ms-excel; charset=utf-8");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: inline; filename="dosya.xls"');
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sayfa</title></head>
<body>
<table>
  <tr>
    <td align="left">BAŞLIK 1</td>
    <td align="left">BAŞKIK 2</td>
    <td align="left">BAŞLIK 3</td>
  </tr>
  <tr>
    <td align="left">içerik</td>
    <td align="left">içerik</td>
    <td align="left">içerik</td>
  </tr>
</table>
</body></html>

 

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);

?>