合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
​ 前端代码: ~~~html <div style="background-color: hotpink;margin-top: 30px"> <form enctype="multipart/form-data" action="{:url('llnumbers/import')}" method="post"> <input type="file" class="btn-file" name="excel" /> <br> <input type="submit" class="btn-danger" value="上传" /> </form> </div> ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 后台代码: ~~~php /** * 导入 */ public function import() { vendor('PHPExcel'); vendor('PHPExcel.PHPExcel_IOFactory'); vendor('PHPExcel.PHPExcel_Cell'); //实例化PHPExcel $objPHPExcel = new \PHPExcel(); $file = request()->file('excel'); // $this->success('123'); if ($file) { $file_types = explode(".", $_FILES ['excel'] ['name']); // ["name"] => string(25) "excel文件名.xls" $file_type = $file_types [count($file_types) - 1];//xls后缀 $file_name = $file_types [count($file_types) - 2];//xls去后缀的文件名 /*判别是不是.xls文件,判别是不是excel文件*/ if (strtolower($file_type) != "xls" && strtolower($file_type) != "xlsx") { echo '不是Excel文件,重新上传'; die; } $info = $file->move(ROOT_PATH . 'public' . DS . 'excel');//上传位置 $path = ROOT_PATH . 'public' . DS . 'excel' . DS; $file_path = $path . $info->getSaveName();//上传后的EXCEL路径 //echo $file_path;//文件路径 $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel = $objReader->load($file_path, $encode = 'utf-8'); //读入数据,转换为数组格式 $re = $objPHPExcel->getsheet(0)->toArray(); $success=0; $error=0; foreach ($re as $key=>$value){ if($value[0]!='phone'){ $ck=Db::name('llnumbers')->where('phone',$value[0])->find(); if($ck){ $error+=1; continue; } $intdata=$this->cs($value[0]); $intdata['yuc']=0; $intdata['price']=$value[1]; $intdata['typestatus']=$value[2]; $result = Db::name('llnumbers')->insert($intdata); if($result){ $success+=1; }else{ $error+=1; } } } $this->success('导入成功'.$success.'条,失败'.$error.'条'); }else{ $this->error('导入失败'); } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 1. 网上下载PHPExcel包,[http://www.php.cn/xiazai/leiku/1491](http://cssnb.com/go/aHR0cDovL3d3dy5waHAuY24veGlhemFpL2xlaWt1LzE0OTE= "http://www.php.cn/xiazai/leiku/1491") 下载后只需要Classes目录下的文件即可 2. 放在Thinkphp/vendor/下面,前端直接跳转到该控制器 3. 这里是目录结构,这是下载出来的,只需要Classes文件下的 4. ![2019-04-20T03:07:32.png](https://imgconvert.csdnimg.cn/aHR0cDovL2Nzc25iLmNvbS91c3IvdXBsb2Fkcy8yMDE5LzA0LzYyMzYzMTE1OC5wbmc?x-oss-process=image/format,png)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")​编辑 ![2019-04-20T03:07:43.png](https://imgconvert.csdnimg.cn/aHR0cDovL2Nzc25iLmNvbS91c3IvdXBsb2Fkcy8yMDE5LzA0LzM5NDMyODEyNjIucG5n?x-oss-process=image/format,png)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")​编辑 ​