当前位置: 移动技术网 > IT编程>开发语言>PHP > tp3.2和Bootstrap模态框导入excel表格数据

tp3.2和Bootstrap模态框导入excel表格数据

2018年12月05日  | 移动技术网IT编程  | 我要评论

导入按钮

<button class="btn btn-info" type="button" id="import" data-toggle="modal" data-target="#mymodal">导入</button>

模态框

<!-- modal -->
    <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="mymodallabel">导入</h4>
                </div>
                <div class="modal-body">
                    <div>导入格式如下</div>
                    <table class="table table-bordered">
                        <tr>
                            <th>编号</th>
                            <th>姓名</th>
                            <th>手机号</th>
                            <th>性别</th>
                            <th>出生日期</th>
                        </tr>
                        <tr>
                            <td>1</td>
                            <td>小李</td>
                            <td>18888888888</td>
                            <td>男</td>
                            <td>2013年12月20日</td>
                        </tr>
                    </table>

                    <form action="#" method="post" id="file-form" enctype="multipart/form-data">
                        <div class="form-group">
                            <label class=" control-label" style="width:85px;">上传文件<sup>*</sup></label>
                            <div class="">
                                <input type="file" name="excel" style="display:block;" />
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="file-import">确定</button>
                    <!--<button type="button" class="btn btn-danger" id="edituser">确定</button>-->
                </div>
            </div>
        </div>
    </div>

弹出模态框,选择文件,点击确定

//导入
    $('#file-import').on('click',function(){
        var fileflag = false;
        fileflag = $("input[name='excel']").val();
        if(!fileflag) {
            alert( '请选择文件!');
            return false;
        }
        // 创建
        var form_data = new formdata();
        // 获取文件
        var file_data = $("input[name='excel']").prop("files")[0];
        // 把所以表单信息
        form_data.append("excel", file_data);
        $.ajax({
            url:'/admin/import',
            type:'post',
            datatype:'json',
            processdata: false,  // 注意:让jquery不要处理数据
            contenttype: false,  // 注意:让jquery不要设置contenttype
            data: form_data,
            success:function(info){
                console.log(info);
                alert(info.msg);
                if(info.code == 1){
                    window.location.reload();
                }
            }
        })
    });

php控制器

    /**
     * 导入
     */
    public function import(){
        $name = substr(strrchr($_files['excel']['name'], '.'), 1);
        $file = $_files['excel']['tmp_name'];
        $data = $this->excel($name,$file);
        if(!$data){
            return $this->ajaxreturn(['code'=>-1,'msg'=>'文件格式不正确!']);
        }
        foreach($data as &$v){
            $datearr = date_parse_from_format('y年m月d日',$v['time']);
            if(!$datearr['year']){
                $v['time'] = '';
                continue;
            }
            $v['time'] = $datearr['year'].'-'.$datearr['month'].'-'.$datearr['day'];
        }
        $allid = m('user')->addall($data);
        if($allid){
            return $this->ajaxreturn(['code'=>1,'msg'=>'导入成功']);
        }
        $this->ajaxreturn(['code'=>-2,'msg'=>'导入失败']);
    }

    /**
     * 读表格信息
     */
    public function excel($name,$files){
        //导入phpexcel类库,因为phpexcel没有用命名空间,只能inport导入
        import("common.vendor.excel.phpexcel");
        //创建phpexcel对象,注意,不能少了\
        $phpexcel=new \phpexcel();
        if ($name == 'xls') {
            //如果excel文件后缀名为.xls,导入这个类
            import("common.vendor.excel.phpexcel.reader.excel5");
            $phpreader=new \phpexcel_reader_excel5();
        }
        if ($name == 'xlsx') {
            //如果excel文件后缀名为.xlsx,导入这下类
            import("common.vendor.excel.phpexcel.reader.excel2007");
            $phpreader=new \phpexcel_reader_excel2007();
        }

        //载入文件
        $phpexcel=$phpreader->load($files);
        $currentsheet=$phpexcel->getsheet(0);
        $allcolumn=$currentsheet->gethighestcolumn();
        $allrow=$currentsheet->gethighestrow();
        //循环读取数据
        for($currentrow=2;$currentrow<=$allrow;$currentrow++){
            $arr['name'] = $phpexcel->getactivesheet()->getcell('b'.$currentrow)->getvalue();
            $arr['mobile'] = $phpexcel->getactivesheet()->getcell('c'.$currentrow)->getvalue();
            $arr['sex'] = $phpexcel->getactivesheet()->getcell('d'.$currentrow)->getvalue();
            $arr['time'] = gmdate("y年m月d日",\phpexcel_shared_date::exceltophp($phpexcel->getactivesheet()->getcell('f'.$currentrow)->getvalue()));
            $data[] =$arr;
        }
        return $data;
    }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网