thinkphp中使用PHPExcel导出数据

首先下载引入PHPEXcel插件文件,我把放在ThinkPHP目录下的Library/Org/Util中。
这里要注意一下,下载的时候那个PHPExcel.php文件需要改成PHPExcel.class.php。
这里写图片描述

下面是使用代码:

<?php
namespace Home\Controller;
use Think\Controller;
class CheckController extends PublicController
{
    protected $check;
    //初始化查询的表
    public function _initialize()
    {
        $this->check = M('check');
    }
    /*
     * 封装PHPExcel导出的方法
     * 文件名$fileName
     * $headArr 字段名称
     * 数据 $data 
     * */
    private  function getExcel($fileName,$headArr,$data){$stime=microtime(true); 
        //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");

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

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

        //设置表头
        $key = ord("A");
        $cdd = '';
        $ll = ord("A");

        $k_arr = array();

        foreach($headArr as $v){
            $colum = chr($key);
            $colum = $cdd.$colum;

            $k_arr[$v] = $colum;

            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            //设置边框
            $sharedStyle1=new \PHPExcel_Style();
            $sharedStyle1->applyFromArray(array('borders'=>array('allborders'=>array('style'=>\PHPExcel_Style_Border::BORDER_THIN))));
            $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1,$colum.'1');
            //背景色填充
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFill()->getStartColor()->setARGB('#C9EDF7');
            $key += 1;


            if($key > 90){
              $cdd = chr($ll);
              $ll += 1;

              $key = ord("A");
            }


            $objPHPExcel->getActiveSheet()->getColumnDimension($colum.'1')->setWidth('20');

        }

        $column = 2;
        $objPHPExcel->getActiveSheet()->getStyle()->getFont()->setName('微软雅黑');//设置字体

        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach($data as $rows){ //行写入
            foreach($rows as $k => $value){// 列写入  
                $objActSheet->setCellValue($k_arr[$k].$column, $value);
            }
            $column++;
        }

        $fileName = iconv("utf-8", "gb2312", $fileName);
         $etime=microtime(true);//获取程序执行结束的时间
         $total=$etime-$stime;   //计算差值
        //重命名表
        //$objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码
        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;
    }
    //调用上面的getExcel方法导出数据
    public function epiexport()
    {
          $Model = M('userinfo');
          $map['Status']=1;
          $userinfolist = $Model->where($map)->select();
         //用户表数据
         $alldata = array();
        foreach ($userinfolist as $k=>$val) {
            $alldata[$k]['Number'] = $val['number'];
            $alldata[$k]['DocuDate'] = $val['docudate'];
            $alldata[$k]['Name'] = $val['name'];
            $alldata[$k]['Sex'] = $val['sex'] == 1 ? '男' : '女';
            $alldata[$k]['Birth'] = $val['birth'];
            $alldata[$k]['Nation'] = $val['nation'];
            $alldata[$k]['FatherName'] = $val['fathername'];
            $alldata[$k]['FatherPhoneNo'] = $val['fatherphoneno'];
            $alldata[$k]['MotherName'] = $val['mothername'];
            $alldata[$k]['MotherPhoneNo'] = $val['motherphoneno'];
            $alldata[$k]['Address'] = $val['address'];
            $alldata[$k]['School'] = $val['school'];
            $alldata[$k]['SchoolType'] = $val['schooltype'];
            $alldata[$k]['ClassName'] = $val['classname'];
            $alldata[$k]['AddTime'] =date('Y-m-d',$val['addtime']);
            $alldata[$k]['Lost']=$val['lost'];
            $alldata[$k]['inquiryopen'] = $val['inquiryopen'];
        }
            $headArr=array_keys($alldata[$k]);  //字段名称
            $filename="epidata"; //导出的文件命名
            $this->getExcel($filename,$headArr,$alldata); //调用getExcel的方法
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值