PHPExcel下载地址: https://github.com/PHPOffice/PHPExcel


代码:

  1. public function excel()
  2. {
  3.     ini_set('memory_limit', '1024M');
  4.     if ($this->request->isPost()) {
  5.         $file = request()->file('file');
  6.         // 上传到本地服务器
  7.         $ext = $file->getOriginalExtension();
  8.         if(!in_array( $ext, $this->option['ext'])){
  9.             return err('请上传xls或者xlsx格式');
  10.         }
  11.         $savename = \think\facade\Filesystem::disk('public')->putFile( 'file', $file);
  12.         //获取文件路径
  13.         $path = public_path().'public/storage/'.$savename;
  14.         if($ext=="xlsx"){
  15.             $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  16.         }else{
  17.             $reader = \PHPExcel_IOFactory::createReader('Excel5');
  18.         }
  19.         $excel = $reader->load($path,$encode = 'utf-8');
  20.  
  21.         $sheet = $excel->getSheet(0)->toArray();
  22.  
  23.         //读取第一张表
  24.         //获取总行数
  25.         array_shift($sheet);  //删除第一个数组(标题);
  26.         $data = [];
  27.         $i = 0;
  28.         foreach ($sheet as $k => $v) {
  29.             if(is_numeric($v[1])){
  30.                 $v[1] = (int)$v[1];
  31.             }
  32.             $data[$k]['username'] = $v[0];
  33.             $data[$k]['password'] = password_hash($v[1],PASSWORD_BCRYPT);
  34.             $data[$k]['num_coin'] = $v[2];
  35.             $data[$k]['draws'] = $v[3];
  36.             $i++;
  37.         }
  38.         $result = $this->model->saveAll($data);
  39.         if($result) return suc('导入成功');
  40.         return err('导入失败');
  41.     } else {
  42.         return err('上传文件失败');
  43.     }
  44. }

引入文件我是放在入口文件的:

  1. include __DIR__ . '/../extend/Classes/PHPExcel.php';
  2. include __DIR__ . '/../extend/Classes/PHPExcel/IOFactory.php';


相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!