probá esto......suerte!
Código PHP:
<?php
header("Pragma: no-cache");
include ("functions/conexion.php"); // inclui la coneccion .... o generala aqui adentro.....
?>
<HTML>
<body>
Código PHP:
<?php
$sql = "SELECT * FROM tabla1 ";
$sql .= " WHERE campo2 = '$id2' ";
$sql .= " ORDER BY campo3";
$result = mysql_query($sql);
$shtml .= "<table width='718' cellpadding='1' cellspacing='1'>";
$shtml .= "<tr>";
$shtml .= "<td colspan=9 height='80'>";
$shtml .= "Titulo: "; // aca pone el titulo
$shtml .= "</td>";
$shtml .= "</tr>";
$shtml .= "<tr>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>1</b></div></td>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>2</b></td>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>3</b></td>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>4</b></td>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>5</b></td>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>6</b></td>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>7</b></td>";
$shtml .= "<td width='100' height='18' style='padding: 2' bgcolor='#C0C0C0' align='left'><font size='2' face='Arial'><b>8</b></td>";
$shtml .= "</tr>";
while ($row = mysql_fetch_row($result)) {
$shtml .= "<tr>";
$shtml .= "<td width='100' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[1] </td>";
$shtml .= "<td width='100' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[0] </td>";
$shtml .= "<td width='100' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[2] </td>";
$shtml .= "<td width='100' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[3] </td>";
$shtml .= "</td>";
$shtml .= "<td width='100' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[5] </td>";
$shtml .= "<td width='100' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[6] </td>";
$shtml .= "<td width='100' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[7] </td>";
$shtml .= "<td width='250' bgcolor='#E1E1E1' style='padding: 2' height='25' align='left'><font size='2' face='Arial'>$row[8] </td>";
$shtml .= "</tr>";
$shtml .= "<tr>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "<td height='1' bgcolor='#cccccc'></td>";
$shtml .= "</tr>";
}
$shtml .= "</table>";
$scarpeta="excel/";
//debe tener permisos 775 por lo menos
$sfile=$scarpeta."".strftime("%H_%M_%S").".xls"; //ruta del archivo a generara
$fp=fopen($sfile,"w");
fwrite($fp,$shtml);
fclose($fp);
echo "<script language='javascript'>";
echo "location.href= '$sfile'";
echo "</script>";
?>
</body>
</html>