数据导入
2016-01-13 11:30
260 查看
1.jsp
2.js
3.java的action层
4.实现层
<button type="button" id="importBtn2" class="btn btn-mini btn-primary" rel="tooltip" data-placement="top" data-original-title="导入" data-html="true" data-toggle="modal" data-target="#uploadModal" style="margin-right:10px;"> <i class="fa fa-upload">导入数据</i> </button>
<!-- Modal --> <div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <form class="form-horizontal" id="uploadForm" action="../../itsm/FlowEngine/uploadFile.action" enctype="multipart/form-data" method="post"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="uploadModalLabel"><i class="fa fa-plus-square fa-fw"></i>导入数据</h4> </div> <div class="modal-body"> <div class="form-group"> <label class="col-sm-3 control-label">请选择导入文件:</label> <div class="col-sm-6"> <input type="text" name="fileName" id="fileName" class="form-control" disabled="disabled" > <input type="file" name="importFile" id="importFile" style="display:none" onchange="document.getElementById('fileName').value=this.value"> <input type="hidden" name = "uploadid" id = "uploadid" value = "<%=expnum%>"> <input type="hidden" name = "updownflowid" id = "updownflowid" value = "<%=flowlistid%>"> <input type="hidden" name = "updowncode" id = "updowncode" value = "<%=code%>"> <input type="hidden" name = "updowndotname" id = "updowndotname" value = "<%=dotname%>"> <button type="button" class="btn btn-mini btn-primary" id="exploreButton" onclick="document.getElementById('importFile').click();">浏览</button> </div> </div> </div> <div class="modal-footer" style=" border-top: 1px solid #e5e5e5;margin-top: 15px;padding: 19px 20px 20px;text-align: right;background-color:white"> <button type="submit" class="btn btn-info" id="importData"><i class="fa fa-check"></i> 确认</button> <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> 取消</button> </div> </form> </div> </div> </div>
2.js
//初始化ajax form var options = { beforeSubmit:beforeSubmit, // callback success:osuccess // callback }; $('#uploadForm').ajaxForm(options).submit(function(){return false}); //验证文件格式是否正确 function beforeSubmit(){ var fileName = ""+$("#fileName").val(); var extStar = fileName.lastIndexOf(".")+1; var fileType = fileName.substring(extStar,fileName.length); if(fileType == "xls"){ $.SmartMessageBox({ title : "", content : '<div id="div-wait" style="text-align: center; margin-top:40px"></br><p>导入中......</p></div>', buttons : '[HiddenButton]', }); $("#img-wait").css({"display":"inline"}); $("#div-wait").prepend($("#img-wait")); $("#bot1-Msg1").css({"display":"none"}); } else{ alertError("错误","请选择xls文件"); return false; } } function osuccess(data){ var start = data.indexOf(">"); if (start != -1) { var end = data.indexOf("<", start + 1); if (end != -1) { data = data.substring(start + 1, end); } } var da =eval('(' + data + ')'); if(da.success == "true"){ //页面上隐藏的img标签在第一次导入后会被移除,需要再次创建 var path=""; path = location.pathname; path = path.substr(0,path.search(/newplatform/)); $("#form-import-close").click(); $("#img-wait").css({"display":"none"}); $("#row2").after("<img style='display:none' id='img-wait' alt='' src='"+path+"newplatform/newassets/common/img/wait.gif'>"); $("#form-export").after($("#img-wait")); $("#bot1-Msg1").click(); $(".curSelectedNode").click(); $('#uploadModal').modal('hide'); window.location.href=window.location.href; $.bigBox({ title : "成功", content : "操作完成!", color : "#739E73", icon : "fa fa-check", timeout : 6000 }); }else if(da.success == "titleError"){ $("#form-import-close").click(); $("#img-wait").css({"display":"none"}); $('#uploadModal').modal('hide'); $("#form-export").after($("#img-wait")); $("#bot1-Msg1").click(); $(".curSelectedNode").click(); $.bigBox({ title : "警告", content : "模板表头不符合规范,请下载相关的模板!", color : "#C46A69", icon : "fa fa-warning shake animated", number : "1", timeout : 6000 }); }else{ $("#form-import-close").click(); $("#img-wait").css({"display":"none"}); $('#uploadModal').modal('hide'); $("#form-export").after($("#img-wait")); $("#bot1-Msg1").click(); $(".curSelectedNode").click(); bootbox.confirm("存在错误数据,是否需要下载?", function(result) { if(result){ $.SmartMessageBox({ title:"系统提示:", content:"<br><br>正在下载数据,导出完毕后点击确认关闭", buttons:"[确认]" }) var res = encodeURI(data); res = encodeURI(res); //需要通过两次编码 location.href = "../../itsm/FlowEngine/error_exportFile.action?res="+res; } }); } }
3.java的action层
/** * 获取前台上传的.xls文件的数据以及参数 * @return * @throws Exception */ public String uploadFile() throws Exception{ System.out.println("enter into FlowEngineAction"); //已经存在的数据 int maxnum = Integer.valueOf(this.getRequest().getParameter("uploadid")); String flowid = this.getRequest().getParameter("updownflowid"); String updowncode = this.getRequest().getParameter("updowncode"); WorkListBean wlb=this.workListDAO.find(Integer.valueOf(flowid)); //导入数据 List<Map<String,String>> resultStr = flowDAO.importData(importFile); //数据无误的情况下,保存数据库 StringBuilder json = new StringBuilder(); if(tmpflag){ json.append("{\"success\":\""+flag+"\",\"res\":\""+resultStr+"\"}"); }else{ String tflg = "titleError"; json.append("{\"success\":\""+tflg+"\",\"res\":\""+resultStr+"\"}"); } print(json.toString()); return NONE; }
4.实现层
//导入数据 public List<Map<String,String>> importData(File importFile) throws Exception{ System.out.println("enter into FlowDAOImpl"); List<Map<String,String>> datas = new ArrayList<Map<String,String>>(); //创建工作簿 Workbook wb = Workbook.getWorkbook(importFile); //获取sheet Sheet sheet = wb.getSheet(0); //遍历row,取出每一个单元格的数据,生成对应的bean写入数据库 for(int row=1;row<sheet.getRows();row++) { Map<String,String> data = new HashMap<String, String>(); data.put("sourceip", sheet.getCell(0, row).getContents()); data.put("sourceport", sheet.getCell(1, row).getContents()); data.put("destip", sheet.getCell(2, row).getContents()); data.put("destport", sheet.getCell(3, row).getContents()); data.put("action", sheet.getCell(4, row).getContents()); data.put("desc", sheet.getCell(5, row).getContents()); datas.add(data); } return datas; }
相关文章推荐
- Extjs4.0 最新最全视频教程
- Javascript中toFixed方法的改进
- 5个常见可用性错误和解决方案
- js可突破windows弹退效果代码
- JSP脚本漏洞面面观
- 使用BAT一句话命令实现快速合并JS、CSS
- js显示当前星期的起止日期的脚本
- 爆炸式的JS圆形浮动菜单特效代码
- js select常用操作控制代码
- JS实现不使用图片仿Windows右键菜单效果代码
- 从jsp发送动态图像
- 原生js结合html5制作小飞龙的简易跳球
- js 页面模块自由拖动实例
- js实现小鱼吐泡泡在页面游动特效
- js 提交和设置表单的值
- PHP VBS JS 函数 对照表
- node.js抓取并分析网页内容有无特殊内容的js文件
- PHP+JS实现大规模数据提交的方法
- 仿51JOB的地区选择效果(可选择多个地区)
- js身份证验证超强脚本