Thinkphp 导出大量数据 csv格式

public function test2() {
        $user_count = M('department')->count();

        $page = ceil($user_count / 10000);

        $fp = fopen('c:\file.csv', 'w');
        for ($p = 1; $p <= $page; $p++) {

            $user = M('department')->page($p . ',10000')->select();

            foreach ($user as $fields) {

                foreach ($fields as $i => $v) {

                    $fields[$i] = iconv('utf-8', 'gbk', $v);
                }

                fputcsv($fp, $fields);
            }

            unset($user);
        }

        fclose($fp);

    }

 

 

public function exportseller($begindate = '', $enddate = '', $areaid = 0, $search = '') {
        ini_set('memory_limit','1024M');
        set_time_limit ( 0 );
        $where = array (); // 筛选条件集合
        $areaid = session ( 'authority' ); // 区域经理角色限定查看区域
        // 选择起止时间后,才显示记录
        if (isset ( $_GET ['begindate'] ) && isset ( $_GET ['enddate'] )) {
            $begindate = I ( 'get.begindate' );
            $enddate = I ( 'get.enddate' );
            $begindate .= " 00:00:00";
            $enddate .= " 23:59:59";
            $where ['r.createtime'] = array (
                'between',
                array (
                    $begindate,
                    $enddate
                )
            );

            if (FALSE === empty ( $_GET ['search'] )) {
                $search = I ( 'get.search' );
                $where ['i.user'] = array (
                    'like',
                    '%' . $search . '%'
                );
            }
            if ($areaid > 0) {
                $where ['s.id_area'] = $areaid;
            } elseif (FALSE === empty ( $_GET ['areaid'] )) {
                $areaid = I ( 'get.areaid' );
                $where ['s.id_area'] = $areaid;
            } else {
            }
            $record = D ( 'Record' );
            $xlsCell = array ('comp'=>'所属公司','sellername'=>'姓名','sellermobile'=>'手机号','sellerjob'=>'岗位','shopno'=>'店铺编号','shopname'=>'店铺名称','areaname'=>'所属市场','prodname'=>'商品名称','sellerjifen'=>'积分','seltime'=>'销售时间','wuliu'=>'物流码','jfcd'=>'积分码','card_num'=>'银行卡号','FGoodsCode'=>'产品编号','FGoodsName'=>'产品名称','FBillDate'=>'出入库时间','FClientCode'=>'经销商编号','FClientName'=>'经销商名称','istoo'=>'是否匹配');
            $list = $record->sellerReport ( $where );
            foreach ( $list as $i => $row ) {
                $webdata=M('WebserviceData');
                $map['box_code']=array('eq',$row['wuliu']);
                $arr=$webdata->where($map)->find();
                //$arr = $this->webServerData ( $row ['wuliu'] );
                $row ['FGoodsCode'] = $arr ['FGoodsCode'];
                $row ['FGoodsName'] = $arr ['FGoodsName'];
                $row ['FBillDate'] = $arr ['FBillDate'];
                $row ['FClientCode'] = $arr ['FClientCode'];
                $row ['FClientName'] = $arr ['FClientName'];
                $row ['istoo'] = $arr ['FClientCode'] == $row ['shopno'] ? '匹配' : '不匹配';
                $str[$i]=array_iconv($row);
            }
            array_unshift($str,array_iconv($xlsCell));
            header ( "Content-type:application/vnd.ms-excel" );
            header ( "Content-Disposition:filename=" . iconv ( "UTF-8", "GB18030", "美容师统计报表".date('_YmdHis') ) . ".csv" );
            $fp = fopen('php://output', 'a');
            foreach ($str as $key=>$line)
            {
                fputcsv($fp,$line);
            }
            fclose($fp);
//            \Admin\Model\ExcelModel::exportExcel ( '美容师统计报表', $xlsCell, $list );
        }
        die();
        //end modify
        $this->display();
    }

 

posted on 2017-03-02 18:44  长不大的菜鸟  阅读(2307)  评论(0编辑  收藏  举报

导航