thinkphp3.2+PHPExcel导出查询数据到excel表格的实例

首先下载PHPExcel插件,我们需要把PHPExcel.php和PHPExcel文件夹放到D:XAMPPhtdocsfsxbThinkPHPLibraryVendorPHPExcel目录下,然后主题代码实现:

    public function download(){
        Vendor('PHPExcel.PHPExcel');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        // 首先创建一个新的对象  PHPExcel object
        $objPHPExcel = new PHPExcel();
         $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        // 位置aaa  *为下文代码位置提供锚
             $objPHPExcel->setActiveSheetIndex(0)  
                            ->setCellValue('A1', '序号')  
                            ->setCellValue('B1', '商户姓名')  
                            ->setCellValue('C1', '商户名称')  
                            ->setCellValue('D1', '商户号')  
                            ->setCellValue('E1', '终端号')  
                            ->setCellValue('F1', '序列号')  
                            ->setCellValue('G1', '机型')  
                            ->setCellValue('H1', '代理商')
                            ->setCellValue('I1', '手机号')
                            ->setCellValue('J1', '人单时间');

            $startTime=I('get.startTime');
            $endTime=I('get.endTime');
            $agent=I('get.agent');
            $model=I('get.model');
            $istrue=I('get.istrue')==0?0:1;  
                if ($startTime!=''&&$endTime!='') {
                $str=$startTime.",".$endTime;
                $where['time']=array('between',$str);
                }
            if ($agent!='') {
                $where['agent']=$agent;
                }
            if ($model!='') {
                $where['model']=$model;
                }
            if ($istrue==1) {
                $where['istrue']=$istrue;
                }
            $data=M('consumer')->where($where)->select();
            if ($data)  
            {  
                $count=1;  
                foreach($data as $v)
                {  
                    $count+=1;  
                    $l1="A"."$count";  
                    $l2="B"."$count";  
                    $l3="C"."$count";  
                    $l4="D"."$count";  
                    $l5="E"."$count";  
                    $l6="F"."$count";  
                    $l7="G"."$count";  
                    $l8="H"."$count";  
                    $l9="I"."$count";  
                    $l10="J"."$count";  

                    $objPHPExcel->setActiveSheetIndex(0)              
                                ->setCellValue($l1, $v['id'])  
                                ->setCellValue($l2, $v['cname'])  
                                 ->setCellValue($l3, $v['mname'])  
                                ->setCellValue($l4, " ".$v['cnumber'])  
                                 ->setCellValue($l5, $v['tnumber'])  
                                ->setCellValue($l6, $v['snumber'])  
                                 ->setCellValue($l7, $v['model'])  
                                ->setCellValue($l8, $v['agent'])  
                                 ->setCellValue($l9, $v['phone'])   
                                ->setCellValue($l10, $v['time']);
                }  
            }          

        //得到当前活动的表,注意下文教程中会经常用到$objActSheet
        $objActSheet = $objPHPExcel->getActiveSheet();
        // 位置bbb  *为下文代码位置提供锚
        // 给当前活动的表设置名称
        $objActSheet->setTitle('list');
        // 生成2007excel格式的xlsx文件
         header("Content-Type: application/force-download");

                header("Content-Type: application/octet-stream");

                header("Content-Type: application/download");

                header("Content-Disposition:inline;filename='consumer.xlsx");

                header("Content-Transfer-Encoding: binary");

                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

                header("Pragma: no-cache");

                $objWriter->save('php://output');

                exit;
        }
原文地址:https://www.cnblogs.com/hltswd/p/4953093.html