您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 兴安分类信息网,免费分类信息发布

ThinkPhp5.1 + PHPExcel制作数据导入

2024/3/3 14:42:44发布11次查看
数据导入,在很多地方都会有需要,如何使用thinkphp5.1制作数据导入,接下来小编带大家去了解整个过程。
1 准备工作
小编是通过phpexcel实现数据导入的,所以在制作之前首先需要下载phpexcel相关组件,目前tp5.1支持composer安装,小编就是通过composer安装的phpexcel组件。【推荐:thinkphp视频教程】
在安装之前首先需要确定自己的电脑上有composer组件,如果还没有安装 composer,在 linux 和 mac os x 中可以运行如下命令:
curl -ss https://getcomposer.org/installer | phpmv composer.phar /usr/local/bin/composer
在 windows 中,你需要下载并运行 composer-setup.exe,下载地址:
https://www.kancloud.cn/manual/thinkphp5_1/353948
安装好composer之后,就是安装phpexcel了,win+r,打开运行界面,输入cmd,进入管理界面,输入你的项目所在的盘符(此处以d盘为例,项目在d:\phpstudy_pro\www\myapp.io),然后点击回车:
输入:cd d:\phpstudy_pro\www\myapp.io,定位到项目所在目录
接下来就是安装phpexcel插件,输入:composer require phpoffice/phpexcel,点击回车,即可开始安装phpexcel。
2 前端提交页面
html
<form class="layui-form" enctype="multipart/form-data"> <input type="hidden" name="type_id" value="{$type_id}"> <div class="layui-form-item" style="margin-left: 42px;"> <div class="layui-input-inline" style="width: 122px;"> <button type="button" class="layui-btn" name="file" lay-verify="file" id="test3"><i class="layui-icon"></i>上传文件</button> </div> </div> <div class="layui-form-item" style="margin-left: 42px;"> <div class="layui-input-inline"> <button class="layui-btn" lay-filter="add" lay-submit="add"> 导入 </button> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <div style="line-height: 35px;"> 注: <p>1.文件大小:请上传小于10m的文件</p> <p>2.文件类型:上传时首先 <span class="common-a"> <a href="/import/member.xlsx">下载导入模板</a> </span>,填好信息后上传</p> </div> </div> </div></form>
js
<script> layui.use(['form', 'layer','upload'], function () { $ = layui.jquery; var form = layui.form , layer = layui.layer; var $ = layui.jquery, upload = layui.upload; upload.render({ elem: '#test3' ,url: '你的上传路径' ,accept: 'file' //普通文件 ,exts: 'xls|xlsx' ,size:'10240' ,done: function(res){ $('#test3').append('<input type="text" name="file" id="file" lay-verify="file" value="'+res.data +'" />') } }); //监听提交 form.on('submit(add)', function(data){ console.log(data); //发异步,把数据提交给php $.post('{:url(\'saveimportmember\')}',$('form').serialize(),function(data){ if(data.res == 1){ layer.msg(data.msg); settimeout(function(){parent.window.location.reload();},1000); }else if(data.res == 0){ layer.alert(data.msg,{icon: 2}); }else{ layer.alert('操作失败',{icon: 2}); } }) return false; }); });</script>
3 后台处理
这里以上传一张会员信息表为例,包含的字段值有:姓名(name)、性别(sex)、会员类型(type_id)、身份证号(identity)、会员编号(number)、联系电话(telephone)、排序(sort)、会员状态(status)。
//上传excel文件$file = request::param('file');//获取文件路径$filepath = env::get('root_path').'public'.directory_separator.$file;if($filepath == ''){ return ['res'=>0,'msg'=>'你上传的文件为空'];}$suffix = $this->dbsy->getfileext($file);//判断哪种类型if($suffix=="xlsx"){ $reader = \phpexcel_iofactory::createreader('excel2007');}else{ $reader = \phpexcel_iofactory::createreader('excel5');}//载入excel文件$excel = $reader->load("$filepath",$encode = 'utf-8');//读取第一张表$sheet = $excel->getsheet(0);//获取总行数$row_num = $sheet->gethighestrow();//获取总列数$col_num = $sheet->gethighestcolumn();$time = time();$data = []; //数组形式获取表格数据$count = 0;$total = 0;$error_count = 0;for ($i = 2; $i <= $row_num; $i ++) { $type_id = request::param('type_id'); $data['type_id'] = $type_id; $name = $sheet->getcell("a".$i)->getvalue(); $sex = $sheet->getcell("b".$i)->getvalue(); $identity = $sheet->getcell("c".$i)->getvalue(); $telephone = $sheet->getcell("f".$i)->getvalue(); $data['sort'] = $this->dbsy->getsort(5,'sort desc',array('type_id'=>$type_id)); if(!$identity){ return ['res'=>0,'msg'=>'身份证号不能为空']; } $data['identity'] = $identity; if(!$name){ return ['res'=>0,'msg'=>'姓名不能为空']; } $data['name'] = $name; if($sex=='男'){ $data['sex'] = 1; }elseif ($sex=='女'){ $data['sex'] = 2; }else{ $data['sex'] = 3; } $data['identity'] = $identity; $data['number'] = $this->dbsy->getnumber(5,'sort desc',array('type_id'=>$type_id)); if($telephone){ $data['telephone'] = $telephone; }else{ $data['telephone'] = ''; } $data['status'] = 5; $member = $this->dbsy->findwhere(5,array('name'=>$name,'identity'=>$identity,'type_id'=>$type_id)); if($member){ /*$data['updatetime'] = time();*/ $info = $this->dbsy->editcontent(5,$data,array('id'=>$member['id'])); if($info){ $total++; } }else{ // 读取单元格 $data['addtime'] = time(); $data['updatetime'] = time(); $info = $this->dbsy->insertgetid(5,$data); if($info){ $count++; }else{ $error_count++; } }}$msg = "成功导入".$count."条数据,重复".$total."条数据,导入失败".$error_count."条数据";if($count > 0){ return ['res'=>1,'msg'=>$msg];}else{ return ['res'=>0,'msg'=>$msg];}
以上就是小编总结的利用tp5.1+phpexcel制作信息导入的所有流程,希望对大家有所帮助。
以上就是thinkphp5.1 + phpexcel制作数据导入的详细内容。
兴安分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录