excle 文件的导入和导出

//excle 文件导出
       public function excel(){
            try{
                     include(BASE_PATH."Excel/PHPExcel.php");
                     // echo BASE_PATH;
                     // echo __URL__;
                    // $test = new test();
                    // $test->index();
                    $objPHPExcel = new PHPExcel();
                    // dump($objPHPExcel);exit;
                    $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                                                 ->setLastModifiedBy("Maarten Balliauw")
                                                 ->setTitle("Office 2007 XLSX Test Document")
                                                 ->setSubject("Office 2007 XLSX Test Document")
                                                 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                                                 ->setKeywords("office 2007 openxml php")
                                                 ->setCategory("Test result file");


                    $model = M("member");
                    
                    $list = $model->alias("m")->field("m.company,m.status,s.*")->join("left join pre_message s on m.id = s.uid")->order("m.id asc")->select();
                    // echo M()->getlastsql();
                    // dump($list);exit;
                    
                  $k1=2;
                  foreach($list as $k=>$v1){
                              if($v1['status'] ==2){
                                 $v1['status'] ="已签到";
                              }else{
                                 $v1['status'] ="未签到";
                              }
                              if($v1['addtime']){
                                  $v1['addtime'] = date("Y-m-d H:i",$v1['addtime']);
                              }
                            
                              if($k !=0){
                                      $k1++;
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$k1,$v1['company']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$k1,$v1['name']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$k1,$v1['duty']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$k1,$v1['phone']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$k1,$v1['email']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$k1,$v1['addtime']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G'.$k1,$v1['status']);
                              }else{               
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1','所属单位名称' );
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1','参与人姓名');
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1','职务');
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1','手机号');
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1','邮箱');
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1','入会时间');
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1','签到状态');
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A2',$v1['company']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B2',$v1['name']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C2',$v1['duty']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D2',$v1['phone']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E2',$v1['email']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F2',$v1['addtime']);
                                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G2',$v1['status']);
                                   
                                    
                              }
                  }



//多图上传方法   及文件上传
    function uploadImg() {
        import('ORG.Net.UploadFile');
        $upload = new UploadFile();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        // $upload->allowExts  = array('jpg','png','jpeg');// 设置附件上传类型
        $upload->saveRule  = "";// 设置附件上传类型
        $savepath='./Upload/Member';

        $arr = explode("/",$savepath);
        foreach($arr as $v){
            if($v != "." ){
                $res  .= "/".$v;
            }
            if(!file_exists(".".$res)){
                mkdir(".".$res);
            }
        }
        
        // echo $res;exit;
        $upload->savePath =  ".".$res."/";// 设置附件上传目录
        if(!$upload->upload()) {// 上传错误提示错误信息
            echo $upload->getErrorMsg();
            // $this->error($upload->getErrorMsg());
        }else{// 上传成功 获取上传文件信息
            $info =  $upload->getUploadFileInfo();
            $uploadList = $upload->getUploadFileInfo();
            $newPath = substr($upload->savePath.$uploadList[0]['savename'], '1');
        }
        $this->excel($newPath);
        // echo $newPath;
        //print_r(J(__ROOT__.'/'.$info[0]['savepath'].'/'.$info[0]['savename']));
    }

//excle文件  导入
public function excel($file=""){
        /** Include path **/
        // set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');
        /** PHPExcel_IOFactory */
        header("Content-type:text/html;charset=utf-8");
        // echo $file;exit;

        // include "/Excel/PHPExcel/IOFactory.php";
        include "Excel/PHPExcel/IOFactory.php";
        // echo $file;exit;
        if($file){
            $inputFileName = ".".$file;  //上传成功路么为不带 。的绝对路径 
            // $inputFileName = $file;  //上传成功路么为不带 。的绝对路径 
            $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
            $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
            // dump($sheetData);exit;
            //数据处理,上传到表中
            $model = new MemberModel();
            // dump($model);
            echo $model->add_member($sheetData);
        }else{
            echo "文件为空";
        }
        
    }

 

你可能感兴趣的:(excle 文件的导入和导出)