This is simple code for exporting student table to excel.
Hope this helps you.
<?
mysql_connect('localhost','root','');
mysql_select_db('test');
$sql = "select name,sirname from student";
// Query Database
$result=mysql_query($sql);
$filename = 'file.xls';
// Send Header
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");
// XLS Data Cell
xlsBOF();
xlsWriteLabel(0,0,"Name : ");
xlsWriteLabel(0,1,"Sirname :");
$xlsRow = 1;
while(list($name,$sirname)=mysql_fetch_row($result))
{
++$i;
xlsWriteLabel($xlsRow,0,"$name");
xlsWriteLabel($xlsRow,1,"$sirname");
$xlsRow++;
}
xlsEOF();
exit();
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
?>