摘要:不适用插件,直接使用php
/**
* TODO 导出CSV文件
*
* @param array $data
* @param array $header_data
* @param string $file_name
*/
function e_export_csv($data = array(), $header_data = array(), $file_name = '')
{
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$file_name);
header('Cache-Control: max-age=0');
$fp = fopen('php://output', 'a');
if (!empty($header_data)) {
foreach ($header_data as $key => $value) {
$header_data[$key] = iconv('utf-8', 'gbk', $value);
}
fputcsv($fp, $header_data);
}
$num = 0;
//每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
$limit = 100000;
//逐行取出数据,不浪费内存
$count = count($data);
if ($count > 0) {
for ($i = 0; $i < $count; $i++) {
$num++;
//刷新一下输出buffer,防止由于数据过多造成问题
if ($limit == $num) {
ob_flush();
flush();
$num = 0;
}
$row = $data[$i];
foreach ($row as $key => $value) {
$row[$key] = iconv('utf-8', 'gbk', $value);
}
fputcsv($fp, $row);
}
}
fclose($fp);
exit(0);
}