1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > tp5 使用phpexcel将excel表格的导入数据库

tp5 使用phpexcel将excel表格的导入数据库

时间:2019-04-25 10:49:03

相关推荐

tp5 使用phpexcel将excel表格的导入数据库

1--使用composer安装phpexcel包

composer require phpoffice/phpexcel -vvv

2-控制器

//导入数据public function insertExcel(){if(request() -> isPost()){vendor("PHPExcel.PHPExcel"); //方法一$objPHPExcel =new \PHPExcel();//获取表单上传文件$file = request()->file('excel');$info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public'); //上传验证后缀名,以及上传之后移动的地址 E:\wamp\www\bick\publicif($info){// echo $info->getFilename();$exclePath = $info->getSaveName(); //获取文件名$file_name = ROOT_PATH . 'public' . DS . $exclePath;//上传文件的地址$objReader =\PHPExcel_IOFactory::createReader("Excel");$obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8$excel_array=$obj_PHPExcel->getSheet(0)->toArray(); //转换为数组格式array_shift($excel_array); //删除第一个数组(标题);$city = [];$i=0;foreach($excel_array as $k=>$v) {$city[$k]['id'] = $v[0];$city[$k]['phone'] = $v[1];$city[$k]['username'] = $v[2];$city[$k]['nickname'] = $v[3];$city[$k]['end_time'] = $v[4];$city[$k]['level'] = $v[5];$i++;}Db::name("users")->insertAll($city);}else{echo $file->getError();}}return $this->fetch("user-excel");}

3-视图

<form action="{:url('admin/user/insertExcel')}" method="post" class="form form-horizontal" enctype="multipart/form-data"><div class="row cl"><label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>导入数据:</label><div class="formControls col-xs-8 col-sm-9"><input type="file" name="excel" /></div></div><div class="row cl"><div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3"><input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;"></div></div></form>

参考地址:/he_he/thinkphp5

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。