function exportToCsv($fileName, $data)
{
header("Content-Type: application/vnd.ms-excel; charset=GB2312");
header("Content-Disposition: attachment; filename=" . $fileName);
header('Cache-Control: max-age=0');
$str = '';
foreach ($data as $row) {
$str_arr = array();
foreach ($row as $column) {
$str_arr[] = $column . "\t";
}
$str .= self::charset(implode(',', $str_arr) . PHP_EOL);
}
echo $str;
ob_flush();
flush();
unset($str, $str_arr);//释放内存
exit();
}
调用测试
$data = [
['序号','名称','电话'],
['1','张三','13112345678'],
['2','李四','13212345678'],
['3','王五','13312345678'],
['4','张三的邻居','13412345678'],
['5','李四的邻居','13512345678'],
['6','王五的邻居','13612345678'],
];
exportToCsv('test.csv',$data);