TP导入Excel

 public function userimport()
    {
        if (IS_POST) {
            //判断是否符合文件格式
            $allowedExts = array("xls", "xlsx", "csv");
            $temp = explode(".", $_FILES["f"]["name"]);
            $extension = end($temp);     // 获取文件后缀名
            if (!in_array($extension, $allowedExts)) {
                $this->error('文件类型不允许,请重新操作');
            }
            $upload = new \Think\Upload();
            $info = $upload->upload();
            $filePath = "../Uploads/" . $info['f']['savepath'] . $info['f']['savename'];
            import("IOFactory", "../excel/toexcel/PHPExcel", ".php");
            import("Excel5", "../excel/toexcel/PHPExcel/Reader", ".php");
            import("PHPExcel", "../excel/toexcel", ".php");

            $PHPExcel = \PHPExcel_IOFactory::load($filePath);
            $date = new \PHPExcel_Shared_Date();
//        $PHPExcel = $reader->load($filePath);
//            //读取excel文件中的第一个工作表
            $sheet = $PHPExcel->getSheet(0);
//            //取得最大的行号
            $allRow = $sheet->getHighestRow();
            $count = 0;
            $error_note = '';
            for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
                $name = $PHPExcel->getActiveSheet()->getCell('A' . $currentRow)->getValue();//工号
                $password = $PHPExcel->getActiveSheet()->getCell('B' . $currentRow)->getValue();
                $nickname = $PHPExcel->getActiveSheet()->getCell('C' . $currentRow)->getValue();
                $dept = $PHPExcel->getActiveSheet()->getCell('D' . $currentRow)->getValue();
                $email = $PHPExcel->getActiveSheet()->getCell('E' . $currentRow)->getValue();
                $mobile = $PHPExcel->getActiveSheet()->getCell('F' . $currentRow)->getValue();
                $weixin = $PHPExcel->getActiveSheet()->getCell('G' . $currentRow)->getValue();
                $birthday = $PHPExcel->getActiveSheet()->getCell('H' . $currentRow)->getValue();
                $sex = $PHPExcel->getActiveSheet()->getCell('I' . $currentRow)->getValue();
                $roles = $PHPExcel->getActiveSheet()->getCell('J' . $currentRow)->getValue();//角色
                $role_ls = $PHPExcel->getActiveSheet()->getCell('K' . $currentRow)->getValue();//角色等级
                //生日
                if ($birthday) {
                    $birth = $date::ExcelToPHP($birthday);
                    $birthdays = date('Y-m-d', $birth);
                } else {
                    $birthdays = "";
                }
                if ($sex == '男') {
                    $sex = 1;
                } elseif ($sex == '女') {
                    $sex = 2;
                } else {
                    $sex = 0;
                }
                //密码
                if (empty($password)) {
                    $password = '123';
                }


                /* //邮箱
                if(empty($email)){
                    $error_note .= '第' . $currentRow . '行,导入失败:' . '邮箱不能为空;' . "</br>";
                    continue;
                }
                $ucem = M('UcenterMember')->where('email = '.$email)->getfield('email');
                if($ucem){
                    $error_note .= '第' . $currentRow . '行,导入失败:' . '邮箱已存在;' . "</br>";
                    continue;
                }
               //手机
                if(empty($mobile)){
                    $error_note .= '第' . $currentRow . '行,导入失败:' . '手机号不能为空;' . "</br>";
                    continue;
                }
                $mobile = M('UcenterMember')->where('mobile = '.$mobile)->getfield('email');
                if($mobile){
                    $error_note .= '第' . $currentRow . '行,导入失败:' . '手机号已存在;' . "</br>";
                    continue;
                }*/
                //部门
                if (empty($dept)) {
                    $error_note .= '第' . $currentRow . '行,导入失败:' . '部门不能为空;' . "</br>";
                    continue;
                }
                $depts = explode('/', $dept);
                //专业服务部 / 商业项目 / 项目一组
                $pid = 0;
                foreach ($depts as $vo) {
                    static $pid = 0;
                    $deptids = M('dept')->field('did,parent')->where(array('name' => trim($vo), 'parent' => $pid))->find();
                    if ($deptids) {
                        $deptid = $deptids['did'];
                        $pid = $deptids['did'];
                    } else {
                        $deptid = 0;
                        break;
                    }
                }
                if (empty($deptid)) {
                    $error_note .= '第' . $currentRow . '行,导入失败:' . '部门不存在;' . "</br>";
                    continue;
                }
                /*  //工号
                  if (empty($name)) {
                      $error_note .= '第' . $currentRow . '行,导入失败:' . '工号不能为空;' . "</br>";
                      continue;
                  }*/
                //姓名
                if (empty($nickname)) {
                    $nickname = $email;
                }
                //微信
                if (empty($weixin)) {
                    $weixin = '';
                }
                //角色
                $parent = 0;
                $roleid = 0;
                if (!empty($roles)) {
                    $roles = explode('/', $roles);
                    foreach ($roles as $vo) {
                        static $parent = 0;
                        $authgroup = M('AuthGroup')->field('id,parent')->where(array('title' => trim($vo), 'parent' => $parent))->find();
                        /*header("Content-type:text/html;charset=utf-8");
                            echo M()->getlastsql();echo "</br>";*/
                        if ($authgroup) {
                            $roleid = $authgroup['id'];
                            $parent = $authgroup['id'];
                        } else {
                            $roleid = 0;
                            break;
                        }
                    }
                }
                //角色等级
                $rlid = 0;
                if (!empty($role_ls) && $roleid) {
                    $rlid = M('RoleLevel')->where(array('agid' => $roleid, 'name' => trim($role_ls)))->getfield('rlid');
                }


                $api = new UserApi();
                $res_insert = $api->register($email, $password, $email, $mobile);
                if ($res_insert > 0) {
                    $count++;
                    $info = array('uid' => $res_insert, 'nickname' => $nickname, 'sex' => $sex, 'job_num' => $name,
                        'birthday' => $birthdays, 'qq' => $weixin, 'reg_time' => time(), 'status' => 1, 'dept' => $deptid, 'rlid' => $rlid);
                    $uid = M('member')->add($info);
                    if ($uid) {
                        if ($rlid) {
                            M('auth_group_access')->add(array('uid' => $uid, 'group_id' => $roleid, 'rlid' => $rlid));
                        }
                    } else {
                        $error_note .= '第' . $currentRow . '行1,导入失败' . "</br>";
                    }
                } else {
                    $error_note .= '第' . $currentRow . '行,导入失败,' . $this->showRegError($res_insert) . "</br>";

                }
            }
            if ($error_note) {
                $this->error($error_note, U('User/index'));
            } else {
                $this->success('导入成功' . $count . '条数据', U('User/index'));
            }
        } else {

            $root_mbx = array();
            $root_mbx[] = array('title' => '管理', 'url' => U('ProjectConfig/index'));
            $root_mbx[] = array('title' => '人员', 'url' => U('User/index'));
            $root_mbx[] = array('title' => '导入文件', 'url' => '');
            $this->assign('root_mbx', $root_mbx);
            $this->display();
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值