tp5 导入excel数据 到mysql数据库

1. html页面代码:




    
    导入数据


 

2. 后台接收代码:

	public function save(){
       if(request() -> isPost())
       {
           vendor("PHPExcel.PHPExcel"); 
           $objPHPExcel =new \PHPExcel();

           //var_dump($objPHPExcel);die;
           //获取表单上传文件
           $file = request()->file('excel');
           //print_r($file);die;
           $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public/file');  //上传验证后缀名,以及上传之后移动的地址  

           if($info)
           {

               $exclePath = $info->getSaveName();  //获取文件名
               $file_name = ROOT_PATH . 'public/file/' . DS . $exclePath;//上传文件的地址
               $objReader =\PHPExcel_IOFactory::createReader("Excel2007");
               $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
               $excel_array=$obj_PHPExcel->getSheet(0)->toArray();   //转换为数组格式

               //print_r($excel_array);die;
               array_shift($excel_array);  //删除第一个数组(标题);
               $city = [];
               $i=0;
               foreach($excel_array as $k=>$v) {

               	   $click_time  = strtotime($v[2]);//点击时间转为时间戳
               	   $active_time = strtotime($v[2]);//激活时间转为时间戳
                   $city[$k]['adid']      = $v[0];
                   $city[$k]['idfa']      = $v[1];
                   $city[$k]['udid']      = $v[4];
                   $city[$k]['model']     = $v[5];
                   $city[$k]['os']        = $v[6];
                   $city[$k]['ip']        = $v[7];
                   $city[$k]['add_time']  = $click_time;
                   $city[$k]['act_time']  = $active_time;

                   $i++;
               }

              // print_r($city);die;
               Db::name("check_udid")->insertAll($city);
           }else
           {
               echo $file->getError();
           }
       }
   }

 

Tp5 框架项目代码:

链接:https://pan.baidu.com/s/1QM1uMBQ1jZokbV-ebmeHJQ 
提取码:wohj 

 

你可能感兴趣的:(php,后台判断)