tp5导入excel表格

导入
  1. 使用composer安装phpexcel包
composer require phpoffice/phpexcel -vvv
  1. 控制器
 //导入数据  为ajax上传
    public function import(){
     
        if(request() -> isPost())
        {
     
            vendor("PHPExcel.PHPExcel"); //方法一
            $objPHPExcel =new \PHPExcel();
            //获取表单上传文件
            $file=\request()->file("file");

            $savePath = ROOT_PATH . 'public' . DS . 'uploads'. DS . 'excel';//上传文件的地址
            $info = $file->validate(['ext' => 'xlsx,xls'])->move($savePath);  //上传验证后缀名,以及上传之后移动的地址  D:\phpStudy\PHPTutorial\WWW\test\public\uploads\
            if($info)public\uploads\
            {
     
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = $savePath.'/'.$exclePath;//上传文件的地址
                $objReader =\PHPExcel_IOFactory::createReader("Excel2007");
                $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
                $excel_array=$obj_PHPExcel->getSheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                $list = [];
                $i=0;
                foreach($excel_array as $k=>$v) {
     
                    $list[$k]['phone'] = $v[0];
                    $list[$k]['nickname'] = $v[1];
                    $i++;
                }
                $result = Db::name("user")->insertAll($list);
                if ($result){
     
                    return json(["code"=>1,"msg"=>"导入成功"]);
                }else{
     
                    return json(["code"=>0,"msg"=>"导入失败"]);
                }

            }else
            {
     

                return json(["code"=>0,"msg"=>$file->getError()]);
            }
        }
  1. 前端页面
        <form class="layui-form"  enctype="multipart/form-data">
            <div class="layui-form-item">
                <label for="file" class="layui-form-label">
                    <span class="x-red">*</span>excel</label>
                <div class="layui-input-inline">
                    <input type="file"  name="file" id="file" required="" lay-verify="required" autocomplete="off" class="layui-input"></div>
            </div>

            <div class="layui-form-item">
                <label for="file" class="layui-form-label">
                </label>
                <button  class="layui-btn"  lay-submit="import"  lay-filter="import">
                    导入
                </button>
            </div>
        </form>

<script>
	layui.use(['form', 'layer'],
    function() {
     
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        form.verify({
     
            required: [/[\S]+/, "必填项不能为空"],
        });

        form.on('submit(import)', function (data) {
     
            var formData = new FormData();
            formData.append("file", $("#file").get(0).files[0]);
            
            $.ajax({
     
                url: "{:url('admin/user/import')}",
                type: "POST",
                data:formData,
                cache: false,
                contentType: false,    //不可缺
                processData: false,    //不可缺
                success: function (data) {
     
                    if(data.code==1){
     
                        layer.msg(data.msg,{
     icon:1});
                        setTimeout(function () {
     
                            parent.layer.closeAll();
                            parent.location.reload();
                        },1500);
                    }else{
     
                        layer.msg(data.msg,{
     icon:2});
                    }
                },
                error: function () {
     
                    layer.msg("System Service Busy!", {
     offset: '200px'});
                }
            });
            return false;
        });

    });
</script>

你可能感兴趣的:(php,php)