te paso este script:
Código PHP:
<?
$sql = "SELECT * FROM tabla";
$result = mysql_query($sql)or die("1 ".mysql_error());
$fields = mysql_num_fields($result);//aquí contamos el número de fields en la tabla
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($result, $i) . "\t";
}//aquí vamos tomando los nombre de los fields
$resultCat=mysql_query($sql)or die("2 ".mysql_error());
while($rowCat = mysql_fetch_array($resultCat)) {
$sql="SELECT DISTINCT CatId FROM tabla";
$result = mysql_query($sql)or die("3 ".mysql_error());
while($row = mysql_fetch_row($result)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
}
$data = str_replace("\r","",$data);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
lo ejecutas y te crea el excel