PHP开发的一个小小项目,记录下实现思路(附上实现功能的示例代码)(三)

接着上次的功能:

   Excle表的导入:

   前端页面:

<form action="__URL__/impUser" method="POST" enctype="multipart/form-data">
		<select name="select" id="" class="form-control">
			<option value="1">基本信息</option>
			<option value="2">工作情况</option>
			<option value="3">奖惩情况</option>
			<option value="4">资助情况</option>
			<option value="5">xxxxxxx</option>
			<option value="6">xxxxxxx</option>
		</select>
<input type="file" class="form-control" name="file" id="file1"/>

<div class="modal-footer">
  <button type="button" class="btn btn-default" data-dismiss="modal">关闭
  </button>
  <input type="submit" class="btn btn-primary" id="sureChange" value="确认导入" />			
</div>
</form>

控制器中:

function impUser(){
        if($_FILES['file']['name']==''){
            echo "<script type='text/javascript'>alert('导入文件不能为空');</script>";
            $URL=U('admin/TheList/index');
            echo "<script type='text/javascript'>
            window.location.href = '$URL';
            </script>";
            exit;
        }

	D(Member)->impUser();
}

模版中:

function impUser(){
    $impType = $_POST['select'];  //接收select,用于选择导入类型
        if (!empty($_FILES)) {
            $config=array(
                'allowExts'=>array('xlsx','xls'),
                'savePath'=>'./Public/upload/',
                'saveRule'=>'time',
            );
            $upload = new \Think\UploadFile($config);
            if (!$upload->upload()) {
                $this->error($upload->getErrorMsg());
            } else {  // 上传成功 获取上传文件信息
                $info = $upload->getUploadFileInfo();  
                
            }
    //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
		import("Org.Util.PHPExcel");
		import("Org.Util.PHPExcel.Reader.Excel5");
		import("Org.Util.PHPExcel.Reader.Excel2007");
		import("Org.Util.PHPExcel.IOFactory.php");
                import("Org.Util.PHPExcel.Shared.Date.php");

                /*有文件上传引用上传类,返回文件名*/             
                
                $file=$info['0']['savepath'].$info['0']['savename'];  
                if($file){                 
                 //导入excel类                                        
                 /*获取Excel文件类型,确定版本*/                 
                 $extend=pathinfo($file);         //返回文件路径的信息          
                 $extend = strtolower($extend["extension"]);     //将字符串转化为小写            
                 $extend=='xlsx'?$reader_type='Excel2007':$reader_type='Excel5';                 
		 $objReader =  new \PHPExcel_IOFactory;
		 $objReader = $objReader::createReader($reader_type);
                 if(!$objReader){                     
                 //执行失败,直接抛出错误中断          
                    echo "<script type='text/javascript'>alert('抱歉!excel文件不兼容。');</script>";
                               //echo "<script type='text/javascript'>alert($value);</script>";
                               $URL=U('admin/TheList/index');
                               echo "<script type='text/javascript'>
                               window.location.href = '$URL';
                               </script>";
                               exit;       
                 }             

                 $objPHPExcel= $objReader->load($file);    
                 $objWorksheet= $objPHPExcel->getActiveSheet();                 
                 $highestRow= $objWorksheet->getHighestRow();                   
                 $highestColumn = $objWorksheet->getHighestColumn();                  
                 $highestColumnIndex = new \PHPExcel_Cell;
	         $highestColumnIndex=$highestColumnIndex::columnIndexFromString($highestColumn);//总列数  

                 $headtitle =array();
                 for($cols =0 ;$cols<=$highestColumnIndex;$cols++){                         
                    $headtitle[$cols] =(string)$objWorksheet->getCellByColumnAndRow($cols, 1)->getValue();                  
                 }                 
                 if(empty($headtitle[0])){
                       for($cols =0 ;$cols<=$highestColumnIndex;$cols++){                         
                       $headtitle[$cols] =(string)$objWorksheet->getCellByColumnAndRow($cols, 2)->getValue();                     
                       }                 
                 }                                          
                 $strs=array(); 
                                             
                 /*第二行开始读取*/                 
                 for ($row =1;$row <= $highestRow;$row++){                     
                     for($cols =0 ;$cols<=$highestColumnIndex;$cols++){                         
                     $strs[$row][$cols] =(string)$objWorksheet->getCellByColumnAndRow($cols, $row)->getValue();                     
                     }                 
                 } 

                 switch($impType){
                    
                   case '1':  //基本信息  OK
                   $stu = D('information');  //插入数据库
                   $flag=1;
                   for($row =3;$row <= $highestRow;$row++){
                      $cols=0;
                      if($strs[$row][$cols]==Null && $strs[$row][$cols+1]==Null){
                         continue;
                      }
                      $check=0;
                      $row2=2;
		      $stu->Name = $where2['Name']=$strs[$row][$cols++];
                      $stu->Sex = $where2['Sex']=$strs[$row][$cols++];       
                      $where['Stu_number']=$where2['Stu_number']=$strs[$row][$cols];
		      //此处的Stu_number为信息表中的学号
                      $stu->Stu_number = $strs[$row][$cols++];
                      $stu->Major = $where2['Major']=$strs[$row][$cols++];
                      $stu->Grade = $where2['Grade']=$strs[$row][$cols++];
                      $stu->Class = $where2['Class']=$strs[$row][$cols++];
                      $stu->ID_number = $where2['ID_number']=$strs[$row][$cols++];
                      $stu->Nation = $where2['Nation']=$strs[$row][$cols++];
         //找得到对应学号则添加,多次导入会覆盖
         //找不到则新创建
		      if($stu->where($where)->find()){
                         if(!$stu->where($where)->save($where2)){
                            $result=0;
               	         }
                      }else{
                         $result = $stu->add($where2);
                      }
                      if(!$result){
                         $flag=0;
                      }  
                }

	        if($flag){              
                   echo "<script type='text/javascript'>alert('导入excle表成功');</script>";                  
                   $URL=U('admin/TheList/index');
                   echo "<script type='text/javascript'>
                   window.location.href = '$URL';
                   </script>";
                               
                }else{                         
                   echo "<script type='text/javascript'>alert('导入excle表已覆盖');</script>";
                   $URL=U('admin/TheList/index');
                   echo "<script type='text/javascript'>
                   window.location.href = '$URL';
                   </script>";
                }                                          
       break;

      case ‘2’:
	   如上
       Break;
     }              


猜你喜欢

转载自blog.csdn.net/qq_36289732/article/details/79055228