优化玩家列表导出2

master
chenzhi 5 years ago
parent d3ccc3bb5b
commit 82afa60d12

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

Loading…
Cancel
Save