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

导入按钮

<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;
    }

猜你喜欢

转载自www.cnblogs.com/mthp/p/10064013.html