最新消息: 新版网站上线了!!!

thinkphp3.2导出百万条数据的方法【原创】

<?php
namespace Mobile\Controller;
use Think\Controller;
class DemoController extends Controller {
    public function index(){
        $this->display();
    }
    public function explode_cvs(){
        $count = M('Users')->field('user_id,account,user_name')->count();
        $num = 10000;
        $page = new \Common\Util\CPage($count,$num);
        $list = M('Users')->field('user_id,account,user_name')->order("user_id asc")->limit("{$page->firstRow},{$page->listRows}")->select();
        $next = $page->show();
        $titleList = array('user_id', '用户名', '姓名');
        $file = "./00000.csv";
        $res = $this->set_title($titleList,$file);
        if($res) {
          $result =  $this->csv_export($list,$titleList,$file);
          if($result) {
              $j = array('status'=>1,'next'=>$next['next'],'bar'=>round($next['page']/$next['total']*100,2),'total'=>$next['total'],'p'=>$next['p'],'file'=>$file,'backurl'=>U('Mobile/Index/index'));

              echo json_encode($j);exit;
          } else {
              echo 1;
          }
        }
    }
    private function set_title($titleList,$file){
        $csvData = '';
        $nums = count($titleList);
        for ($i = 0; $i < $nums - 1; $i++)
        {
            $csvData .= '"' . $titleList[$i] . '",';
        }
        $csvData .= '"' . $titleList[$nums - 1] . "\"\r\n";
        $csvData = mb_convert_encoding($csvData, "cp936", "UTF-8");
        if(!file_exists($file)) {
            file_put_contents($file,$csvData);
        }
        return true;
    }
    private function csv_export(&$data,$titleList,$fileName = '')
    {
        ini_set("max_execution_time", "3600");
        $csvData = '';
        $nums = count($titleList);
        foreach ($data as $key => $row)
        {
            $i = 0;
            foreach ($row as $_key => $_val)
            {
                $_val = str_replace("\"", "\"\"", $_val);
                if ($i < ($nums - 1))
                {
                    $csvData .= '"' . $_val . '",';
                }
                elseif ($i == ($nums - 1))
                {
                    $csvData .= '"' . $_val . "\"\r\n";
                }
                $i++;
            }
            unset($data[$key]);
        }
        $csvData = mb_convert_encoding($csvData, "cp936", "UTF-8");
        file_put_contents($fileName,$csvData,FILE_APPEND);
        return true;
    }
    public function download($file){
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header("Content-type:text/csv;");
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            @unlink($file);
            exit;
        }
    }
}

转载请注明出处

转载请注明:谷谷点程序 » thinkphp3.2导出百万条数据的方法【原创】