Prima dovresti mettere gli header e poi quello che ti genera... Ti posto un esempio di quello che a me funziona (l'ho chiamato excel.php):
Codice PHP:
<?php
$filename="tabella.xls";
header ("Content-Type: application/vnd.ms-excel");
header ("Content-Disposition: inline; filename=$filename");
$search = mysql_query("SELECT * FROM `tabella` WHERE 1=1");
echo "<h1>Magazzino</h1>
<table border='1'>
<tr>
<th>Documento di trasporto</th>
<th>Società</th>
<th>Data</th>
<th>Materiale</th>
<th>Quantità</th>
</tr>";
while($r = mysql_fetch_array($search)){
echo "<tr>";
echo "<td>" . $r['ddt'] . "</td>";
echo "<td>" . $r['societa'] . "</td>";
echo "<td>" . $r['data'] . "</td>";
echo "<td>" . $r['materiale'] . "</td>";
echo "<td>" . $r['quantita'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Prova ad impaginarlo direttamente in una tabella (se puoi)...