tp3/tp5 PHPExcel实现表格导入数据库

管理员 发布于 6年前   394

tp3.2

首先下载phpexcel放入vendor 其它就不说了直接贴代码
   public function impUser(){
		if (!empty($_FILES)) {
			$config=array(
                'exts'=>array('xlsx','xls'),
                'rootPath'=>"./Public/",
                'savePath'=>'Uploads/',
                'saveName'    =>   $_FILES['name'],
                'subName'    =>    array('date','Ymd'),
			);
			$upload = new \Think\Upload($config);
            if (!$info=$upload->upload()) {
                $this->error($upload->getError());
			}/*else {
				//$info = $upload->getUploadFileInfo();
			}
            */
            //var_dump($_FILES);exit;
			vendor("PHPExcel.PHPExcel");
			$file_name=$upload->rootPath.$info['import']['savepath'].
			$info['import']['savename'];
			//var_dump($file_name);exit;
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			for($i=3;$i<=$highestRow;$i++)
			{	
				//对应行列插入就ok了
				$data['username'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['password'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['email'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['regtime'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['lastlogin']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['status']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['sessionid']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				//print_r($data);exit;
				M('member')->add($data);	
			}
			$this->success('导入成功!');
		}else{
			$this->error("请选择上传的文件");
		}	
	}

tp5

phpexcel放入extend
    public function impuser()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('import');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $zj = 1024*1024*1;   
            //,'ext'=>'jpg,png,gif'
            $info = $file->validate(['size'=>$zj])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            }else{
                // 上传失败获取错误信息
                 $this->error($file->getError());
            }
            //var_dump('./public/uploads/'.$path);
            //ob_end_clean();
            Loader::import('PHPExcel.PHPExcel');
            $file_name = ROOT_PATH .'public/uploads/'.$info->getSaveName();
            $file_name = str_replace('\\', '/', $file_name);
            //var_dump($file_name);exit;
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow(); // 取得总行数
            $highestColumn = $sheet->getHighestColumn(); // 取得总列数
            for($i=3;$i<=$highestRow;$i++)
            {   
                //对应行列插入就ok了
                $data['username'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue()?
                                    $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue():'';
                $data['password'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue()?
                                    $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue():'';
                $data['email']  =   $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue()?
                                    $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue():'';
                $data['regtime'] =  $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue()?
                                    $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue():'';
                $data['lastlogin']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue()?
                                    $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue():'';
                $data['status']=    $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue()?
                                    $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue():'';
                $data['sessionid']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue()?
                                    $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue():'';
                //print_r($data);exit;
                db('member')->insert($data); 
            }
            $this->success('导入成功!');
        }else {
            $this->error("请选择上传的文件");
        }
   }


请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客