header ( "Content-type:application/vnd.ms-excel" );
header ( "Content-Disposition:filename=" . iconv ( "UTF-8", "GB18030", "query_user_info" ) . ".csv" );
// 打开PHP文件句柄,php://output 表示直接输出到浏览器
$fp = fopen('php://output', 'a');
// 将中文标题转换编码,否则乱码
foreach ($column_name as $i => $v) {
$column_name[$i] = iconv('utf-8', 'GB18030', $v);
}
// 将标题名称通过fputcsv写到文件句柄
fputcsv($fp, $column_name);
$pre_count = 10000;
for ($i=0;$i<intval($total_export_count/$pre_count)+1;$i++){
$export_data = $db->getAll($sql." limit ".strval($i*$pre_count).",{$pre_count}");
foreach ( $export_data as $item ) {
$rows = array();
foreach ( $item as $export_obj){
$rows[] = iconv('utf-8', 'GB18030', $export_obj);
}
fputcsv($fp, $rows);
}
// 将已经写到csv中的数据存储变量销毁,释放内存占用
unset($export_data);
ob_flush();
flush();
}
exit ();
转自: http://blog.csdn.net/cdy102688/article/details/21395835