if (!function_exists('export_excel')) {
/**
* 数据导出Excel(csv文件)
* @param string $file_name 文件名称
* @param array $tile 标题
* @param array $data 数据源
*/
function export_excel($file_name, $tile = [], $data = [])
{
ini_set('memory_limit', '512M');
ini_set('max_execution_time', 0);
ob_end_clean();
ob_start();
header("Content-Type: text/csv");
header("Content-Disposition:filename=" . $file_name);
$fp = fopen('php://output', 'w');
// 转码 防止乱码(比如微信昵称)
fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
fputcsv($fp, $tile);
$index = 0;
foreach ($data as $item) {
if ($index == 1000) {
$index = 0;
ob_flush();
flush();
}
$index++;
fputcsv($fp, $item);
}
ob_flush();
flush();
ob_end_clean();
}
}