This is an example of what you need. Be sure there is nothing before the "<?header..." - not even a space. The echo statement before the while puts column names in the first row of the excel file.
<?header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: inline; filename=\"contacts.xls\"");
//pconnect file
include "access.php";
$q="select contact_id, companyname, address from contacts";
$r=mysql_query($q);
echo ("Contact Id". "\t" ."Company Name". "\t" ."Address"."\n");
while ($a=mysql_fetch_array($r)) {
echo ($a["contact_id"]. "\t" .$a["companyname"]. "\t"
.$a["address"]. "\n");
}//end while
?>