PHPExcel下载地址: https://github.com/PHPOffice/PHPExcel
代码:
- public function excel()
- {
- ini_set('memory_limit', '1024M');
- if ($this->request->isPost()) {
- $file = request()->file('file');
- // 上传到本地服务器
- $ext = $file->getOriginalExtension();
- if(!in_array( $ext, $this->option['ext'])){
- return err('请上传xls或者xlsx格式');
- }
- $savename = \think\facade\Filesystem::disk('public')->putFile( 'file', $file);
- //获取文件路径
- $path = public_path().'public/storage/'.$savename;
- if($ext=="xlsx"){
- $reader = \PHPExcel_IOFactory::createReader('Excel2007');
- }else{
- $reader = \PHPExcel_IOFactory::createReader('Excel5');
- }
- $excel = $reader->load($path,$encode = 'utf-8');
- $sheet = $excel->getSheet(0)->toArray();
- //读取第一张表
- //获取总行数
- array_shift($sheet); //删除第一个数组(标题);
- $data = [];
- $i = 0;
- foreach ($sheet as $k => $v) {
- if(is_numeric($v[1])){
- $v[1] = (int)$v[1];
- }
- $data[$k]['username'] = $v[0];
- $data[$k]['password'] = password_hash($v[1],PASSWORD_BCRYPT);
- $data[$k]['num_coin'] = $v[2];
- $data[$k]['draws'] = $v[3];
- $i++;
- }
- $result = $this->model->saveAll($data);
- if($result) return suc('导入成功');
- return err('导入失败');
- } else {
- return err('上传文件失败');
- }
- }
引入文件我是放在入口文件的:
- include __DIR__ . '/../extend/Classes/PHPExcel.php';
- include __DIR__ . '/../extend/Classes/PHPExcel/IOFactory.php';
友情提示:垃圾评论一律封号...