主页 > 编程 > php >

Phpgrace 查询结果导出到excel代码

2019-01-30 10:43 阅读:99 来源:智宇SEO自媒体

  Phpgrace 查询结果导出到excel
Phpgrace 查询结果导出到excel代码

  1. 下载PHPExecl 类库 放入/tool
Phpgrace 查询结果导出到excel代码

  代码入下:

 if(!empty($_POST['btn3']))
        {
            $today=date("Y-m-d H:i:s");
            $type=$_POST['paytype2'];
            $nopay=$_POST['nopay'];

            $sql="select * from yuwy_user a WHERE id not in("." SELECT a.id FROM (yuwy_user a LEFT JOIN yuwy_pay b ON a.id=b.userid )LEFT JOIN yuwy_type c ON b.typeid=c.id  WHERE b.typeid="."'$type'"."and b.enddate>"."'$today'".")";
            $this->db->query($sql);
            $this->res2=$this->db->queryFetchAll();

            $excel = tool('PHPExcel');
            //基础信息
            $excel->getProperties()->setCreator("phpGrace")
                ->setLastModifiedBy("phpGrace")
                ->setTitle("phpGrace demo")
                ->setSubject("objPHPExcel");
            //设置 sheet 名称
            $excel->getActiveSheet(0)->setTitle('业主欠费表');
            //标题
            $excel->setActiveSheetIndex(0)
                ->setCellValue('A1', '编号')
                ->setCellValue('B1', '姓名')
                ->setCellValue('C1', '门牌');

            //数据填充【此数据可以来自数据库】

            $i = 2;
            if(empty($this->res2))
            {
                e('查询结果为空');
            }else
            {
                p($this->res2);
                foreach( $this->res2 as $r){
                    $excel->setActiveSheetIndex(0)
                        ->setCellValue('A'.$i, $r['id'])
                        ->setCellValue('B'.$i, $r['name'])
                        ->setCellValue('C'.$i, $r['numberplate']);
                    $i++;
                }
                $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
                $objWriter->save('t.xls');
            }
        }
        }


excel:
Phpgrace 查询结果导出到excel代码