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

layui导入excel文件的方法是什么

2025/10/3 16:04:46发布8次查看
我们先来看看完成的效果图:
(相关推荐:layui)
具体步骤如下:
首先:导入layui第三方插件js,地址:
https://fly.layui.com/extend/excel/
1、在页面中引入excel.js文件:
//引入excel layui.config({ base: 'layui_ext/', }).extend({ excel: 'excel', });
2、监听头工具栏的点击事件
// 监听头工具栏事件table.on('toolbar(terminalconfig)', function(obj) { var layer = layui.layer; // 添加终端 if(obj.event == 'import'){ layer.open({ type : 1, shade : false, area : [ '350px', '260px' ], title : '导入excel', content : $("#importexcel"), cancel : function() { layer.close(); }, success : function(layero, index) { importexcel(); }, }); }//导入excel结束});//监听头工具栏事件结束
3、importexcel()方法:
//导入方法function importexcel(){ var $ = layui.jquery ,upload = layui.upload; var uploadinst = upload.render({ elem: '#importexcel', /*method: 'post',*/ url: basepath + 'poweruser/importpoweruserdata.action', accept: 'file', //普通文件 exts: 'xls|excel|xlsx', //导入表格 auto: false, //选择文件后不自动上传 before: function (obj) { layer.load(); //上传loading }, choose: function (obj) {// 选择文件回调 var files = obj.pushfile(); var filearr = object.values(files);// 注意这里的数据需要是数组,所以需要转换一下 //console.debug(filearr) // 用完就清理掉,避免多次选中相同文件时出现问题 for (var index in files) { if (files.hasownproperty(index)) { delete files[index]; } } uploadexcel(filearr); // 如果只需要最新选择的文件,可以这样写: uploadexcel([files.pop()]) }, error : function(){ settimeout(function () { layer.msg("上传失败!", { icon : 1 }); //关闭所有弹出层 layer.closeall(); //疯狂模式,关闭所有层 },1000); } });}
4、uploadexcel()方法:
function uploadexcel(files) { try { var excel = layui.excel; excel.importexcel(files, { // 读取数据的同时梳理数据 fields: { 'tid' : 'a', 'inport' : 'b', 'state' : 'c', 'householdnumber' : 'd', 'accountname' : 'e', 'phone' : 'f' } }, function (data) { var arr = new array(); for(i = 1; i < data[0].sheet1.length; i++){ var tt = { cid : selectconcentrator, tid : data[0].sheet1[i].tid, inport: data[0].sheet1[i].inport, state: data[0].sheet1[i].state, householdnumber: data[0].sheet1[i].householdnumber, accountname: data[0].sheet1[i].accountname, phone: data[0].sheet1[i].phone, }; arr.push(tt); } $.ajax({ async: false, url: basepath + 'poweruser/importpoweruserdata.action', type: 'post', datatype: "json", contenttype: "application/x-www-form-urlencoded", data: { data : json.stringify(arr) }, success: function (data) { if(data.success){ layer.msg(data.message); settimeout(function () { layer.closeall(); //疯狂模式,关闭所有层 },1000); //表格导入成功后,重载表格 tableins.reload('testterminalconfigreload',{ url : basepath + 'poweruser/poweruserdatatable.action', page : { limit : 10, // 初始 每页几条数据 limits : [ 10, 20, 30 ] // 可以选择的 每页几条数据 }, where : { cid : selectconcentrator, tid : selectterminal }, parsedata: function(res){ //res 即为原始返回的数据 return { "code": 0, //解析接口状态 "msg": res.message, //解析提示文本 "count": res.total, //解析数据长度 "data": res.data //解析数据列表 }; } }, 'data'); }else{ //表格导入失败后,重载文件上传 layer.alert(data.error+"请重新上传",{icon : 2}); } }, error: function (msg) { layer.msg('请联系管理员!!!'); } }); }); } catch (e) { layer.alert(e.message); }}
方法4是读取数据,并对数据进行梳理。把excel数据解析出来,然后把数据传到后端。后端对数据进行解析,然后插入数据库。成功之后,给前端返回excel导入成功。excel导入成功之后,重载表格,数据就可以在表格里显示了。
以上就是layui导入excel文件的方法是什么的详细内容。
该用户其它信息

VIP推荐

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