IT基地
用户登陆
我要注册
用户名或密码错误
登 陆
注 册
IT基地 >李蓝猫的博客 >我要发文
PHPEXCEL组件集成TP
作者:李蓝猫   发布时间:2018-06-08 17:55:09   点击数:12525

因项目需要excel导出

N年前似乎做过

现针对TP集成PHPExcel-1.8新版组件

下载地址 https://github.com/PHPOffice/PHPExcel

借鉴TP论坛集成方法并在原有方式上进行修改


一、将原包Classes下内容放置Vendor

二、TP自定义方法

public function exportExcel($expTitle,$expCellName,$expTableData){
    $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
    $fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
    $cellNum = count($expCellName);
    $dataNum = count($expTableData);
    vendor("Excel.PHPExcel");

    $objPHPExcel = new \PHPExcel();

    $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

    $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
   // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
    for($i=0;$i<$cellNum;$i++){
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
    }
      // Miscellaneous glyphs, UTF-8
    for($i=0;$i<$dataNum;$i++){
      for($j=0;$j<$cellNum;$j++){
        $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
      }
    }
    // 在本地保存文档
    //\PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5')->save("output.xls"); 

    header('pragma:public');
    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
    header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
    //3.2版本 需要静态类调用前加入‘\’否则会报nofound错误
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');    
    $objWriter->save('php://output');
    exit;
}

三、调用方式

//导出Excel

    function ExcelOut(){
        $typeid = I('typeid')?I('typeid'):0;

        if($typeid){
            $xlsName  = "CodeList";
            $xlsCell  = array(
            array('codeid','ID'),
            array('typeid','备注'),
            array('code','兑换码'),
            array('num','可使用次数'),
            array('starttime','开始时间'),
            array('endtime','结束时间'),
            );


            $activation_code_type_college = M('activation_code_type_college')->where("typeid=$typeid")->find();
            $xlsData  = M('activation_code_college')->where("typeid=$typeid")->Field('codeid,typeid,code,num')->select();
            foreach ($xlsData as $k => $v)
            {
                $xlsData[$k]['typeid']=$activation_code_type_college['remark'];
                $xlsData[$k]['starttime']=date('Y-m-d',$activation_code_type_college['starttime']);
                $xlsData[$k]['endtime']=date('Y-m-d',$activation_code_type_college['endtime']);
            }
            //dump($xlsData);
            $this->exportExcel($xlsName,$xlsCell,$xlsData);
        }

    }
    
  //导入方式--因功能未使用 故没有调试
  
  function impUser(){
        if (!empty($_FILES)) {
            import("@.ORG.UploadFile");
            $config=array(
                'allowExts'=>array('xlsx','xls'),
                'savePath'=>'./Public/upload/',
                'saveRule'=>'time',
            );
            $upload = new UploadFile($config);
            if (!$upload->upload()) {
                $this->error($upload->getErrorMsg());
            } else {
                $info = $upload->getUploadFileInfo();
                
            }
        
            vendor("PHPExcel.PHPExcel");
                $file_name=$info[0]['savepath'].$info[0]['savename'];
                $objReader = PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数
                for($i=3;$i<=$highestRow;$i++)
                {   
                   $data['account']= $data['truename'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();  
                    $sex = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                   // $data['res_id']    = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                    $data['class'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
                    $data['year'] = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                    $data['city']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
                    $data['company']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
                    $data['zhicheng']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
                    $data['zhiwu']= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
                    $data['jibie']= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
                    $data['honor']= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
                    $data['tel']= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
                    $data['qq']= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
                    $data['email']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
                    $data['remark']= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
                    $data['sex']=$sex=='男'?1:0;
                    $data['res_id'] =1;
                    
                    $data['last_login_time']=0;
                    $data['create_time']=$data['last_login_ip']=$_SERVER['REMOTE_ADDR'];
                    $data['login_count']=0;
                    $data['join']=0;
                    $data['avatar']='';
                    $data['password']=md5('123456');              
                    M('Member')->add($data);
         
                } 
                 $this->success('导入成功!');
        }else
            {
                $this->error("请选择上传的文件");
            }    
         

    }


本站部分数据来自软件的自动采集,仅用于学习交流,决不用于商用,如有侵犯您的合法权益,请联系我们删除。
发表技术文档,,记录个人生活,聚集意见领袖,众多IT精英,以我们的技术让开发更便捷。
IT基地版权所有,伪版必究 京ICP备17064610号-1
微信QQ空间QQ好友新浪微博FacebookTwitter