您当前的位置: 首页 > 

壹小俊

暂无认证

  • 1浏览

    0关注

    885博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

TP5.1execl导入数据

壹小俊 发布时间:2019-12-04 18:03:11 ,浏览量:1

//控制器代码
public function import_qrcode(Request $request){
        $getData = $request->param();
        if(!isset($getData['session_key']) || !Cache::get($getData['session_key'])['id']){
            $json = json(['status'=>self::NO,'msg'=>$this->error[0],'data'=>[]]);
        }else{
            $config = [
                'ext' => 'xlsx'
            ];
            $file = $this->request->file('file');
            $info = $file->validate($config)->move('./uploads');
            if ($info) {
                $fileName = $info->getSaveName();
                $filePath = Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$fileName;
                $data = import_excel($filePath);
                array_shift($data);
                $arr = [];
                $list = array();
                foreach ($data as $k => $v) {
                    $arr['sn'] = $v[0];
                    $arr['key'] = $v[1];
                    $arr['value'] = $v[2];
                    $arr['s_id'] = $getData['s_id'];
                    $arr['a_id'] = $getData['a_id'];
                    $arr['stoptime'] = $getData['stoptime'];
                    $arr['ctime'] = time();
                    $i = $arr['sn'] ^ $arr['key'];
                    if($i == $arr['value']){
                        $f = true;
                        foreach ($list as $vs) {
                            if ($vs['value'] == $arr['value']) {
                                $f = false;
                                break;
                            }
                        }
                        if ($f == true) {
                            $list[] = $arr;
                        }
                    }
                }
                if (file_exists($filePath)) {
                    unset($info);
                    unlink($filePath);  //删除文件
                }
                if (empty($list)) {
                    $json =  json(['status' => self::NO, 'msg' => $this->error[13]]);
                }else{
                    $bool = $this->model->add_all($list);
                    if($bool){
                        $json = json(['status'=>self::YES,'msg'=>$this->error[11]]);
                    }else{
                        $json = json(['status'=>self::NO,'msg'=>$this->error[12]]);
                    }
                }
            }
        }
        return $json;
    }
/**
 * 导入excel文件
 * @param  string $file excel文件路径
 * @return array        excel文件内容数组
 */
function import_excel($file){
    ini_set('max_execution_time', '0');
    // Vendor('PHPExcel.PHPExcel');
    require'../extend/PHPExcel/PHPExcel.php';
    try{
        $objReader = new \PHPExcel_Reader_Excel2007();//注意和导出的类不一样哦
    }catch (\Exception $e){
        $this->error($e->getMessage());
    }
    $objPHPExcel = $objReader->load($file); 
    $sheet = $objPHPExcel->getSheet(0); 
    // 取得总行数 
    $highestRow = $sheet->getHighestRow();     
    // 取得总列数      
    $highestColumn = $sheet->getHighestColumn(); 
    //循环读取excel文件,读取一条,插入一条
    $data=array();
    //从第一行开始读取数据
    for($j=0;$jgetCell("$k$j")->getValue();
        } 
    }  
    return $data;
}

导入核心文件,tp5.1不支持之前的vendor和import,使用php的内置require导入就可以了。

关注
打赏
1664335782
查看更多评论
立即登录/注册

微信扫码登录

0.0375s