devi mettere gli header sopra e poi tutto il resto:

Codice PHP:
 header("Content-type: application/vnd.ms-excel"); //vitale!!
header("Content-disposition: prova.xls");
header("Pragma: no-cache");
header("Expires: 0"); 

mysql_connect(localhost"root");
mysql_select_db("db");
$query="SELECT codice, nome, prezzo FROM prodotti";
$prova mysql_query($query);
$row_prova mysql_fetch_assoc($prova );

........