thinkphp5导入excel表

admin3年前PHP534

后端

private function checkFileType($file){
        if(empty($file['name'])) return false;
        if($this->getExtension($file) != 'xlsx' && $this->getExtension($file) != 'xls') return false;
        return true;
    }   
    private function getExtension($file)
    {
        $info = pathinfo($file['name']);
        return $info['extension'];
    }

public function import(){
        set_time_limit (0);
        if (IS_POST) {
            $file = $_FILES['filename'];
            if(!$this->checkFileType($file)){
                $this->error('您上传的文件错误!');
            }
            //上传文件
            $file_name = './uploads/excel/'.rand(10000,99999).'.xlsx';
            $f = move_uploaded_file($file['tmp_name'],$file_name);
            //获取表单上传文件
            if($f){
                // 引入文件
                vendor("PHPExcel.PHPExcel.Reader.Excel2007");
                $objPHPExcel = new \PHPExcel_Reader_Excel2007();
                $objReader = $objPHPExcel ->load($file_name);
                //加载文件内容,编码utf-8
                $obj_PHPExcel = $objPHPExcel->load($file_name, $encode = 'utf-8'); 
                //删除文件
                @unlink($file_name);
                $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                $data = [];
                foreach ($excel_array as $k => $v) {
                    if(intval($v[7]) == 1 && intval($v[8]) == 1){
                        $level = 5;
                    }elseif(intval($v[7]) == 1){
                        $level = 1;
                    }elseif(intval($v[8]) == 1) {
                        $level = 4;
                    }
                    $username = $v[5];
                    $pass = $v[6];
                    $area = $v[1];
                    $market = $v[2];
                    $salename = $v[3];
                    $code = $v[4];
                    //检测手机号,存在的话修改信息,否则新增
                    $checkUserName = M('users')->where([
                        'username' => $username,
                        'lang' => 'cn'
                    ])->find();
                    if(empty($checkUserName)) {
                        $id = M('users')->insertGetId([
                        'username' => $username,
                        'password' => func_encrypt($pass),
                        'password_show' => $pass,
                        'level' => $level,
                        'lan' => 'cn',
                        'reg_time' => time()
                        ]);
                        M('users_list')->insert([
                            'users_id' => $id,
                            'para_id' => 3,
                            'info' => $area,
                            'lang' => 'cn',
                            'add_time' => time(),
                            'update_time' => time()
                        ]);
                        M('users_list')->insert([
                            'users_id' => $id,
                            'para_id' => 4,
                            'info' => $market,
                            'lang' => 'cn',
                            'add_time' => time(),
                            'update_time' => time()
                        ]);
                        M('users_list')->insert([
                            'users_id' => $id,
                            'para_id' => 5,
                            'info' => $salename,
                            'lang' => 'cn',
                            'add_time' => time(),
                            'update_time' => time()
                        ]);
                        M('users_list')->insert([
                            'users_id' => $id,
                            'para_id' => 6,
                            'info' => $code,
                            'lang' => 'cn',
                            'add_time' => time(),
                            'update_time' => time()
                        ]);
                    }else{
                        $id = $checkUserName['users_id'];
                        M('users')->where([
                            'username' => $username,
                            'lang' => 'cn'
                        ])->update([
                            'password' => func_encrypt($pass),
                            'password_show' => $pass,
                            'level' => $level,
                            'update_time' => time()
                        ]);
                        M('users_list')->where([
                            'users_id' => $id,
                            'lang' => 'cn',
                            'para_id' => 3
                        ])->update([
                            'info' => $area,
                            'update_time' => time()
                        ]);
                        M('users_list')->where([
                            'users_id' => $id,
                            'lang' => 'cn',
                            'para_id' => 4
                        ])->update([
                            'info' => $market,
                            'update_time' => time()
                        ]);
                        M('users_list')->where([
                            'users_id' => $id,
                            'lang' => 'cn',
                            'para_id' => 5
                        ])->update([
                            'info' => $salename,
                            'update_time' => time()
                        ]);
                        M('users_list')->where([
                            'users_id' => $id,
                            'lang' => 'cn',
                            'para_id' => 6
                        ])->update([
                            'info' => $code,
                            'update_time' => time()
                        ]);
                    }
                }
                
                //批量插入数据
                $this->success('导入成功');
            }
        }
        return $this->fetch();
    }

前端

<div class="page">
    <div class="fixed-bar">
        <div class="item-title"><a class="back" href="{:url('Member/level_index')}" title="返回列表"><i class="fa fa-arrow-circle-o-left"></i></a>
            <div class="subject">
                <h3>导入会员数据</h3>
                <h5></h5>
            </div>
        </div>
    </div>
    <!-- 操作说明 -->
    <div id="explanation" class="explanation" style="color: rgb(44, 188, 163); background-color: rgb(237, 251, 248); width: 99%; height: 100%;">
        
    <form class="form-horizontal" id="postForm" action="{:url('Member/import')}" method="post" enctype="multipart/form-data">
        <div class="ncap-form-default">
            <dl class="row">
                <dt class="tit">
                    <label for="level_name"><em>*</em>导入文件</label>
                </dt>
                <dd class="opt">
                    <input type="file" name="filename">
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <a href="JavaScript:void(0);" onclick="checkForm();" class="ncap-btn-big ncap-btn-green" id="submitBtn" style="margin-left: 135px;">确认提交</a>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    // 判断输入框是否为空
    function checkForm(){

        // if($('input[name=amount]').val() == ''){
        //     showErrorMsg('消费额度不能为空!');
        //     $('input[name=amount]').focus();
        //     return false;
        // }

        // if($('input[name=discount]').val() == ''){
        //     showErrorMsg('折扣率不能为空!');
        //     $('input[name=discount]').focus();
        //     return false;
        // }

        layer_loading('正在处理');
        $('#postForm').submit();
    }
</script>


相关文章

php原生链接mysql,并读取数据

<?phpheader("Content-type:text/html;charset=utf-8");set_time_limit(0);//连接数据库$con = mys...

opencart教程 上传产品

opencart教程 上传产品

点击左边导航【商品目录】【商品管理】点击右上角加号,新增商品填写数据时,以下是必填分类选择如下...

phpqrcode生成二维码,并在底部加上图片

Vendor('phpqrcode.phpqrcode');$font = ROOT_PATH.'public/static/common/font/hgzb.ttf'...

阿里云虚拟电话保护

下载安装好compser下载DEMO,参考网址https://next.api.aliyun.com/api/Dyplsapi/2017-05-25/BindAxb?spm=a2c4g.1118662...

安装Composer报错php_igbinary.dll' - 找不到指定的模块。

首先去php\php-7.1.13-nts\ext中找,并未发现有php_igbinary.dll文件,然后去该网站下载适合自己的文件 网址:https://windows.php.net/downl...

tp5 HTML formdata 上传文件

php后端//上传     public function upload(){     &nbs...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。