转:
http://www.yiichina.com/topic/4509
function down_xls($data, $keynames, $name='dataxls') {
$xls[] = "<html><meta http-equiv=content-type content=\"text/html; charset=UTF-8\"><body><table border='1'>";
$xls[] = "<tr><td>ID</td><td>" . implode("</td><td>", array_values($keynames)) . '</td></tr>';
$index = 0;
if (preg_match('/MSIE/',$_SERVER['HTTP_USER_AGENT'])) {
$name = rawurlencode($name);
}
foreach($data As $o) {
$line = array(++$index);
foreach($keynames AS $k=>$v) {
$line[] = htmlspecialchars($o[$k]);
}
$xls[] = '<tr><td>'. implode("</td><td>", $line) . '</td></tr>';
}
$xls[] = '</table></body></html>';
$xls = join("\r\n", $xls);
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="'.$name.'.xls"');
die(mb_convert_encoding($xls,'UTF-8','UTF-8'));
}
$user_info=array(
0=>array("uid"=>"12","name"=>"fanguohui","sex"=>"nan"),
1=>array("uid"=>"132","name"=>"yuandahhui","sex"=>"nan"),
2=>array("uid"=>"135","name"=>"hhui","sex"=>"nan"),
);
$key_name=array(
"uid"=>"id",
"name"=>"姓名",
"sex"=>"xingbie",
);
down_xls($user_info,$key_name,"用户信息表");