上传excal并存入数据库

vendor("PHPExcel.PHPExcel");
            $objPHPExcel =new \PHPExcel();
            //获取表单上传文件
            $file = request()->file('file');
            if($file){
            $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public');  //上传验证后缀名,以及上传之后移动的地址  E:\wamp\www\bick\public
            
            if($info)
            {
//              echo $info->getFilename();
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . 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();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);

$data = [];
                foreach($excel_array as $k=>$v) {
                    if(!empty(array_filter($v))){
                    $data[$k]['number'] = $v[0];
                    $data[$k]['activity'] = $v[2];
                    $data[$k]['grade'] = $v[4];
                    $data[$k]['beizhu'] = $v[5];
                    $data[$k]['temp'] = $v[5];
                    $data[$k]['three_id'] = $three_id;
                    $data[$k]['date'] = $yue;
                    $data[$k]['year'] = $year;
                    $data[$k]['xq'] = $xq;
                    $data[$k]['create_time'] = date('Y-m-d H:i:s');
                    $data[$k]['abc'] = time();
                   }
                   // if (count(array_filter($v))==9) {
                   //   return '请检查Excal填写手否完整';
                   // }
                }
                //return dump($data);
                User::name("perform")->insertAll($data);

发表评论

邮箱地址不会被公开。

Captcha Code