您好,欢迎访问一九零五行业门户网

layui如何导入excel文件

layui导入excel文件的方法:首先在页面中引入【excel.js】文件;然后监听头工具栏的点击事件,代码为【title : '导入excel',content : $(#importexcel)】。
本教程操作环境:windows7系统、layui v2.5.7版,dell g3电脑,该方法适用于所有品牌电脑。
layui导入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); }}
推荐(免费):layui教程
以上就是layui如何导入excel文件的详细内容。
其它类似信息

推荐信息