🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $file = $params['local']; if (!$file) { $this->error(__('Parameter %s can not be empty', 'file')); } $filePath = ROOT_PATH . DS . 'public' . DS . $file; if (!is_file($filePath)) { $this->error(__('No results were found')); } $PHPReader = new \PHPExcel_Reader_Excel2007(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_Excel5(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_CSV(); if (!$PHPReader->canRead($filePath)) { $this->error(__('Unknown data format')); } } } $PHPExcel = $PHPReader->load($filePath); //加载文件 $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 $highestRow = $currentSheet->getHighestRow(); // // 取得总列数 $highestColumm = $currentSheet->getHighestColumn(); $i=0; $data=[]; //从第一行开始读取数据 for($j=2;$j<=$highestRow;$j++){ //从A列读取数据 // for($k='A';$k!=$highestColumn;$k++){ /*这是大于26列的数据*/ for($k='A';$k<=$highestColumm;$k++){ /*这是小于26列的数据*/ // 读取单元格 $data[$j][] = $PHPExcel->getActiveSheet()->getCell("$k$j")->getValue(); // $data[$j][] = $sheet->getCell($k.$j)->getValue(); } } foreach ($data as $val) { $dataArray = array( 'house_name'=>trim($val[0]), 'name' => trim($val[1]), 'id_card' => trim($val[2]), 'category_id' => $params['category_id'], 'community'=>trim($val[3]), 'remarks' => trim($val[4]), ); $subsidy_user=Db::name('subsidy_user')->where('id_card',$val[2])->where('category_id',$params['category_id'])->find(); if(!$subsidy_user){ $res = Db::name('subsidy_user')->insert($dataArray); if ($res) { $i++; } } } } if ($i>0) { $this->success('导入成功'); } else { $this->error('导入失败,请检查是否有重复或者模板格式是否正确!'); } } return $this->view->fetch(); } ``` ``` "phpoffice/phpexcel": "^1.8", composer update 就可以了 ```