定义通用导出方法
function export_csv($file_name,$tileArray = [],$dataArray = [])
{
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$file_name );
header('Cache-Control: max-age=0');
$file = fopen('php://output',"a");
$limit = 1000;
$calc = 0;
foreach ($tileArray as $v){
$tit[] = iconv('UTF-8', 'GB2312//IGNORE',$v);
}
fputcsv($file,$tit);
foreach ($dataArray as $v){
$calc++;
if($limit == $calc){
ob_flush();
flush();
$calc = 0;
}
foreach($v as $t){
$tarr[] = iconv('UTF-8', 'GB2312//IGNORE',$t);
}
fputcsv($file,$tarr);
unset($tarr);
}
unset($list);
fclose($file);
exit();
}
调用
public function exportList($list)
{
// 表格标题
$tileArray = ['昵称', '手机号', '备注','注册时间'];
// 表格内容
$dataArray = [];
foreach ($list as $vo) {
$dataArray[] = [
'用户昵称' => $vo['nickName'],
'用户手机号' => $vo['phone'],
'类型' => $vo['credittype'],
'变动数量' => $vo['num'],
'备注' => $vo['remark'],
'添加时间' => $vo['create_time']
];
}
// 导出csv文件
$filename = '用户-' . date('YmdHis');
return export_excel($filename . '.csv', $tileArray, $dataArray);
}