参考地址:https://blog.csdn.net/qq_37138818/article/details/81093302
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\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); //删除第一个数组(标题); $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"); }
参考地址:https://www.kancloud.cn/he_he/thinkphp5