<?php
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;
}
$c=OCILogon("hr", "hr", "orcl9");
if(!$c)
			{
			echo "Невозможно подключится к базе: " . var_dump( OCIError() );
			die();
			}
			
$sql="";
$sql=$sql."select Salary from (select Salary,Max(Hire_Date) Hire_date from ";
$sql=$sql."(select Hire_Date, Salary from";
$sql=$sql." (select Hire_Date, Salary from HR.EMPLOYEES order by Hire_date desc, Salary desc)";
$sql=$sql." where RowNum<=50)";
$sql=$sql." Group by Salary";
$sql=$sql." order by 2 desc)";
$sql=$sql." where rowNum<20";
$s = OCIParse($c, $sql);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
			$sal[$i]=ociresult($s,"SALARY");
			$i++;
			}

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=list.xls");
header("Content-Transfer-Encoding: binary ");

xlsBOF(); //начинаем собирать файл
/*первая строка*/
xlsWriteLabel(1,0,"Номер");
xlsWriteLabel(1,1,"З/п");
for($i=0;$i<count($sal);$i++)
	{
	xlsWriteLabel($i+2,0,$i);
	xlsWriteLabel($i+2,1,$sal[$i]);
	}

xlsEOF(); //заканчиваем собирать
?>