csv 导出
set_time_limit(0);
ini_set('memory_limit', '1024M');
// 输出Excel文件头,可把user.csv换成你要的文件名
header('Content-Type: application/vnd.ms-excel;charset=utf-8');
header('Content-Disposition: attachment;filename=csv' . date('Y-m-d H:i:s') . '.csv');
header('Cache-Control: max-age=0');
// 待导出信息
$page_size = 10000;
// 打开PHP文件句柄,php://output 表示直接输出到浏览器
$fp = fopen('php://output', 'a');
// 输出Excel头信息
$head = [
'姓名',
'课程',
'分类',
'报考院校',
'报考机构',
'身份证号',
'批次',
'站点',
'播放进度14%',
'知识点测试20%',
'登陆次数6%',
'参与互动次数10%',
'普通试卷20%',
'阶段性考核20%',
'综合考核30%',
'总分',
];
foreach ($head as $i => $v) {
$head[$i] = $v;
}
// 将数据通过fputcsv写到文件句柄
fputcsv($fp, $head);
$id=0;
do{
ob_flush();
flush();
$offset = $now_page * $page_size;//偏移量
$where['id'] = array('gt',$id);
$data = Db::name('userlist')->where($where)->limit($page_size)->select();//使用where 比直接使用limit($now_page,$page_size)快
if (empty($data)){
break;
}
foreach ($data as $key=>$value){
$csv=[];
$csv[]=$value['realname'];
$csv[]=$value['les_name'];
$csv[]=$value['cate_name'];
$csv[]=$value['sname'];
$csv[]=$value['iname'];
$csv[]=$value['id_card'];
$csv[]=$value['type'];
$csv[]=$value['site'];
$csv[]=$value['hour_complete_num']."/".$value['hour_num']."课时".$value['limit_time_total_str']."/".$value['play_time_total_str']."时长"."百分比".$value['play_rate_total'];
$csv[]=$value['knowledge_answer_num']."/".$value['knowledge_num']."百分比".$value['knowledge_score'];
$csv[]=$value['login_num'];
$csv[]=$value['inquiry_num'];
$csv[]=$value['exam_type4_answer_num']."/".$value['exam_type4_num']."百分比".$value['exam_type4_score'];
$csv[]=$value['exam_type2_num'];
$csv[]=$value['exam_type3_num'];
$csv[]=$value['result_score'];
fputcsv($fp, $csv);
unset($csv);
unset($data);
$id=$value['id'];
}
}while(true);
fclose($fp);
}