PHP excel导入功能

作者: PEIZIJUN | 来源:发表于2017-12-01 14:48 被阅读13次

    excel 导入, 上传文件必须为csv格式的文件,然后读取上传的文件,转换数组,存数据库就好了
    下面最后的 $updata 就是最后处理好的数组,然后存数据库。

    //车辆excel导入
        public function files_car(){
            if($_FILES['file']['error'] == 0){
                $type = trim($_FILES['file']['type']);
                if($type != 'application/vnd.ms-excel'){
                    echo "<script>alert('只能导入csv文件类型!')</script>";die;
                }
                if($_FILES['file']['size'] <= 5000000){
                    $name = $_FILES['file']['name'];
                    $name_arr = explode('.', $name);
                    $new_name = time().uniqid().'.'.$name_arr[count($name_arr)-1];
                    $path = 'upload/excel/'.date('Y',time()).date('m',time()).date('d',time());
                    $new_path = $path.'/'.$new_name;
    
                    if(!is_dir($path)){
                        mkdir($path,0777,true);
                    }
                    if(move_uploaded_file($_FILES['file']['tmp_name'],$new_path)){
                        ob_end_clean();
                        $temp=file($new_path);
                        $data = eval('return '.iconv('gbk','utf-8',var_export($temp,true)).';');
                        for($i=0;$i <count($data);$i++){
                            if($i == 0){
                                $keyarr = explode(',',$data[0]);
                                foreach($keyarr as $v){
                                    if($v == '车主姓名'){
                                        $v = 'username';
                                    }elseif($v == '车牌号'){
                                        $v = 'car_number';
                                    }elseif($v == '续保时间'){
                                        $v = 'car_baotime';
                                    }elseif($v == '到期提醒'){
                                        $v = 'car_remind';
                                    }elseif($v == '续保费用'){
                                        $v = 'money';
                                    }else{
                                        $v = 'content';
                                    }
                                    
                                    $keydata[] = $v;
                                }
                                $keydata[] = 'add_time';
                            }
                            if($i > 0){
                                $daan = explode(',', $data[$i]);
                                for($j=0;$j<count($keydata);$j++){
                                    if($keydata[$j] == 'car_remind'){
                                        $daan[$j] = strtotime($daan[$j]);
                                    }
                                    if($keydata[$j] == 'add_time'){
                                        $daan[$j] = time();
                                    }
                                    $uparr[$keydata[$j]] = $daan[$j];
                                }
                                if($uparr['username'] != ''){
                                    $updata[] = $uparr;
                                }
                            }
                        }
                        foreach($updata as $v){
                            if($v['username'] != ''){
                                $add = db('car')->insert($v);
                            }
                        }
                        if($add){
                            echo 1;die;
                        }
                    }else{
                        echo "<script>alert('上传文件失败!')</script>";die;
                    }
                }else{
                    echo "<script>alert('上传文件过大!')</script>";die;
                }
            }
        }   
    

    相关文章

      网友评论

        本文标题:PHP excel导入功能

        本文链接:https://www.haomeiwen.com/subject/emcwbxtx.html