excel批量导入功能的实现主要是三部分的功能:
1 下载模版
2 数据预览
3 数据导入
工作栏:
模版下载:
选择文件:
数据预览:
姓名 性别 出生年月 行业 级别 是否是农业厅单位 单位 部门 职务 职称 专业领域 处置特长 部门担负职责 应急职责 办公室电话 手机 简介
下载模版调用的函数:
function DownLoad(){ $('#dwForm').submit(); }
//批量导入部分 function penaltyinfodown(){ $('#penaltyinfod').dialog('open').dialog('setTitle','导入excel'); $('#penaltyinfo').form('clear'); }
导入数据函数:
// 批量导入 function implexl(){ var filename=$('#file2').filebox('getValue'); if(filename==null || filename=='') { showMsg('提示','请选择文件!'); return; }else{ $.ajax({ type : 'get', url : '${request.contextPath}/emergencyexpert/loaddata', dataType : 'json', async : true, success : function(data) { if(data==1){ showMsg('提示','数据有误不允许导入!'); }else{ showMsg('提示','导入成功!'); $('#viewdatafm').dialog('close'); $('#penaltyinfod').dialog('close'); $('#dg').datagrid('reload'); } } }); } }
后台实现:
@ResponseBody @RequestMapping("getfilename") public String getFileName(@RequestParam("file2") MultipartFile file, HttpServletRequest request) { String filePath = ""; String fileName = ""; if (!file.isEmpty()) { if (file.getSize() > 0) filePath = FileUtil.upload(file, request, true); fileName = request.getSession().getServletContext() .getRealPath("/") + filePath; } return fileName; }
@ResponseBody @RequestMapping("viewdata") public JqueryUiJson viewdata(String data, HttpServletRequest request, HttpSession session) { mapflag.put("flag", "0"); if(StrUtil.isBlank(data)){ return null;} try { Listlis = new ArrayList (Arrays.asList("expname", "sex", "birth","busitype", "memberlvl","isagriunit", "orgname","deptname", "duty", "prolevel","expdomain","specialwork", "workpart","emergencyduty", "officeno", "phoneno","expdesc","org","dept")); //读取excel表中内容 listflag = StrUtil.viewPre(data, lis, 2); //验证必填项 List lisnull = new ArrayList (Arrays.asList("expname","birth")); listflag = StrUtil.dealViewdata(listflag,mapflag,sqlMapper,4,lisnull); //校验日期 List lisdata = new ArrayList (Arrays.asList("birth")); StrUtil.valiDate(listflag, mapflag, lisdata); //校验手机号码 List lisphone = new ArrayList (Arrays.asList("phoneno")); listflag = StrUtil.valiphone(listflag,mapflag,lisphone); //校验电话号码 List telphone = new ArrayList (Arrays.asList("officeno")); listflag = StrUtil.valitelphone(listflag,mapflag,telphone); //验证组织机构的单位 for(int i=0;i map = sqlMapper.selectOne ("select id,pid from sys_monitor_org " + "where name='"+strs[1]+"'"); String pid2 = map.get("pid").toString(); String id = map.get("id").toString(); //判断前两位填写是否符合格式 if("-1".equals(pid) && "0".equals(pid2) && strs.length > 2){ //从分割的第三项开始比较 for(int t=2;t > lismap2 = sqlMapper.selectList("select id,type from sys_monitor_org " + "where name='"+strs[t]+"' and pid='"+id+"'"); //根据条数是否为空判断数据是否有效 if(null != lismap2 && lismap2.size() == 1){ id = lismap2.get(0).get("id").toString(); //当数据为 if(t == strs.length-1 && "0".equals(lismap2.get(0).get("type").toString())){ listflag.get(i).put("org", id); }else if(t == strs.length-1 && !"0".equals(lismap2.get(0).get("type").toString())){ listflag.get(i).put("orgname", "20error"); mapflag.put("flag", "1"); break; } }else{ listflag.get(i).put("orgname", "20error"); mapflag.put("flag", "1"); break; } } }else{ listflag.get(i).put("orgname", "20error"); mapflag.put("flag", "1"); } } } } //验证组织机构的部门 for(int i=0;i > lismap = sqlMapper.selectList("select id from sys_monitor_org " + "where name='"+listflag.get(i).get("deptname")+"' and pid='"+listflag.get(i).get("org")+"' and type='1'"); if(lismap.size() != 1){ listflag.get(i).put("deptname", "21error"); mapflag.put("flag", "1"); }else{ listflag.get(i).put("dept",lismap.get(0).get("id").toString()); } } }else if(listflag.get(i).get("isagriunit").equals("否") && (null != listflag.get(i).get("deptname") && !"".equals(listflag.get(i).get("deptname"))) && (null == listflag.get(i).get("orgname") || "".equals(listflag.get(i).get("orgname")))){ listflag.get(i).put("orgname", "20error"); mapflag.put("flag", "1"); } } JqueryUiJson jqueryUiJson = new JqueryUiJson(ExampleUtil.getPageInfo(listflag).getTotal(), listflag); return jqueryUiJson; } catch (Exception e) { e.printStackTrace(); return null; } }
1 @ResponseBody 2 @RequestMapping("loaddata") 3 public String loaddata(String data, HttpServletRequest request, 4 HttpSession session) { 5 //返回数据标志位 "1"不允许导入,"0"允许导入 6 String flag = "0"; 7 if(StrUtil.isBlank(data)){ return ConstantUtil .FAILURE;} 8 if("1".equals(mapflag.get("flag"))){ 9 flag="1";10 }else{11 //获取数据库中列表12 Listlisda = StrUtil.getColumnNameStr(sqlMapper,"td_emergency_expert");13 //替换字典表字段14 Map mapdict = new HashMap ();15 mapdict.put("dict_sex", "sex");16 mapdict.put("dict_jobtype", "busitype");17 mapdict.put("dict_role_type", "memberlvl");18 mapdict.put("dict_yes_no", "isagriunit");19 listflag = StrUtil.dealViewdata(listflag, mapflag, sqlMapper, 2,mapdict);20 //添加数据库中的数据21 for(int i=0;i