<?php$result = mysql_query('select * from 表名');
$count = mysql_num_fields($result);for ($i = 0; $i < $count; $i++){
    $header .= mysql_field_name($result, $i)."\t";
}while($row = mysql_fetch_row($result)){
  $line = '';
  foreach($row as $value){
    if(!isset($value) || $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 = "\nno matching records found\n";
}header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");echo $header."\n".$data; ?>