码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp3.1(3.2同理)整合phpExcl 实现数据导出功能

时间:2015-08-04 13:20:16      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

将下载到的phpExcl文件放到org文件夹下面

 

//导出
    public function push(){

        $goods_list = M(‘数据表‘)->select();
        $data = array();
        foreach ($goods_list as $k=>$goods_info){
            $data[$k][id] = $goods_info[‘id‘];
            $data[$k][dingwei] = $goods_info[‘dingwei‘];
            $data[$k][mylocal] = $goods_info[‘mylocal‘];
            $data[$k][tel] = $goods_info[‘tel‘];  
        }

        
        foreach ($data as $field=>$v){
            if($field == ‘id‘){
                $headArr[]=‘ID‘;
            }

            if($field == ‘dingwei‘){
                $headArr[]=‘提交位置‘;
            }
            if($field == ‘mylocal‘){
                $headArr[]=‘污染位置‘;
            }
            if($field == ‘tel‘){
                $headArr[]=‘联系电话‘;
            }
            

            
        }

        $filename="goods_list";

        $this->getExcel($filename,$headArr,$data);
 
        

         
      }



    private  function getExcel($fileName,$headArr,$data){
        //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
        import("Org.PHPExcel");
        import("Org.PHPExcel.Writer.Excel5");
        import("Org.PHPExcel.IOFactory.php");

        $date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";

        //创建PHPExcel对象,注意,不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();

        //设置表头
        $key = ord("A");
        //print_r($headArr);exit;
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.‘1‘, $v);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.‘1‘, $v);
            $key += 1;
        }

        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();

        //print_r($data);exit;
        foreach($data as $key => $rows){ //行写入
            $span = ord("A");
            foreach($rows as $keyName=>$value){// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j.$column, $value);
                $span++;
            }
            $column++;
        }

        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        //$objPHPExcel->getActiveSheet()->setTitle(‘test‘);
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header(‘Content-Type: application/vnd.ms-excel‘);
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header(‘Cache-Control: max-age=0‘);

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
        $objWriter->save(‘php://output‘); //文件通过浏览器下载
        exit;
    }

thinkphp3.1(3.2同理)整合phpExcl 实现数据导出功能

标签:

原文地址:http://www.cnblogs.com/langxi/p/4701566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!