Quizas un ejemplo un poco mas claro para los novatos... pueden verlo asi..
$fecha = date("d-m-Y");
$query_reg = sprintf("select clientes.oficina, clientes.codigo, cdd.porcentaje from cdd INNER JOIN clientes ON clientes.codigo = cdd.codigo order by clientes.oficina, cdd.porcentaje");
$c_reg = mysql_query($query_reg) or die(mysql_error());
$totalRows_reg = mysql_num_rows($c_reg);
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=top10_$fecha.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "<table border=1> ";
echo "<tr> ";
echo "<th>Oficina</th> ";
echo "<th>Codigo</th> ";
echo "<th>Porcentaje</th> ";
echo "</tr> ";
while($row_reg = mysql_fetch_assoc($c_reg)){
echo "<tr> ";
echo "<td>".$row_reg['oficina']."</td> ";
echo "<td>".$row_reg['codigo']."</td> ";
echo "<td>".$row_reg['porcentaje']."</td> ";
echo "</tr> ";
}
echo "</table> ";
?>