发布的php.net函数的改进版本。function download_csv_results($results, $name = NULL){
if( ! $name)
{
$name = md5(uniqid() . microtime(TRUE) . mt_rand()). '.csv';
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "wb");
foreach($results as $result)
{
fputcsv($outstream, $result);
}
fclose($outstream);}
它非常容易使用,并且在MySQL(I)/PDO结果集中工作得很好。download_csv_results($results, 'your_name_here.csv');
记住exit()如果您已经完成了该页,则在调用它之后。