优化玩家列表导出2

master
chenzhi 5 years ago
parent d3ccc3bb5b
commit 82afa60d12

@ -1734,40 +1734,40 @@ function getOffspringByPromoteId($promote_id = null) {
* @return void * @return void
*/ */
function data2csv(&$data,$title,$fields){ function data2csv(&$data,$title,$fields){
 set_time_limit(0); set_time_limit(0);
    ini_set('memory_limit', '-1'); //设置内存不受限制 ini_set('memory_limit', '-1'); //设置内存不受限制
    $csvFileName = $title.'.csv'; $csvFileName = $title.'.csv';
    //设置好告诉浏览器要下载excel文件的headers //设置好告诉浏览器要下载excel文件的headers
    header('Content-Description: File Transfer'); header('Content-Description: File Transfer');
    header('Content-Type: application/vnd.ms-excel;charset=utf-8'); header('Content-Type: application/vnd.ms-excel;charset=utf-8');
    header('Content-Disposition: attachment; filename="'. $csvFileName .'"'); header('Content-Disposition: attachment; filename="'. $csvFileName .'"');
    header('Expires: 0'); header('Expires: 0');
    header('Cache-Control: must-revalidate'); header('Cache-Control: must-revalidate');
    header('Pragma: public'); header('Pragma: public');
    $fp = fopen('php://output', 'a');//打开output流 $fp = fopen('php://output', 'a');//打开output流
    fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));//转码,防止乱码 fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));//转码,防止乱码
    $field = array_keys($fields); $field = array_keys($fields);
    //生成首行 //生成首行
    $header = array_values($fields); $header = array_values($fields);
    fputcsv($fp, $header); fputcsv($fp, $header);
    $index = 0; $index = 0;
    foreach ($data as $row) { foreach ($data as $row) {
        $a = []; $a = [];
        foreach($field as $k=>$v){ foreach($field as $k=>$v){
            // $a[$v] = $row[$v] . "\t";//防止格式错误 // $a[$v] = $row[$v] . "\t";//防止格式错误
            $a[$v] = $row[$v]; $a[$v] = $row[$v];
        } }
        if ($index == 10000) { //每次写入1000条数据清除内存 if ($index == 10000) { //每次写入1000条数据清除内存
            $index = 0; $index = 0;
            ob_flush();//清除内存 ob_flush();//清除内存
            flush(); flush();
        } }
        $index++; $index++;
        fputcsv($fp, $a); fputcsv($fp, $a);
    } }
    ob_flush(); ob_flush();
    fclose($fp);  //每生成一个文件关闭 fclose($fp); //每生成一个文件关闭
    die(); die();
} }
?> ?>

Loading…
Cancel
Save