页面(用的freemaker):
<html lang="en"> <head> <meta charset="UTF-8"> <title>新增入货明细</title> [#include "/include/css.ftl" /] <style> .row{ margin-left:10px !important; } .impInsor{ height:25px; line-height:25px; } .impInsor a{ text-decoration:none; } .needKno > div{ line-height:25px; } </style> </head> <body> <input type="hidden" id="type" value="${type}"> <input type="hidden" id="content" value="${content}"> <input type="hidden" id="cargoInApplyId" value="${cargoInApplyId}"> <div class="row"> <div class="row"> <IFRAME height="50px" width="200px" name="ifm" id="ifm" style="display:none;"></IFRAME> <form action="${base}/cargo_in/cargoInApply/importDetail.do?cargoInApplyId=${cargoInApplyId}" id="uplodFileForm" target="ifm" method="post" enctype="multipart/form-data" class="file-form"> <input type="file" name="fileToUpload" id="fileToUpload" onclick="closeOption();" onchange="onUploadFileChange()" /> </form> <!--<div class="layui-upload"> <button type="button" class="layui-btn layui-btn-normal" id="fileToUpload">选择文件</button> <button type="button" class="layui-btn" id="fileToUploadSub">开始上传</button> </div>--> </div> <div class="row"> <div class="col-xs-10 col-sm-10"> <div id="option" style="color:red"></div> <div id="option2" style="color:red"></div> <div class="impInsor">导入模板下载:<a href="${base}/cargo_in/cargoInApply/download.do?downloadType=1" style="color:blue; text-decoration: underline;">入货申请明细导入模板.xls</a></div> <div id="uploadOk"> <br/> <button type="button" class="btn btn-sm btn-success" onclick="uploadOk()"> <i class="ace-icon fa fa-arrow-circle-o-up "></i> 导入 </button> </div> <div id="inUpload" style="display:none;"> <br/> <font style="font-size:20px" color="green">导入中...请勿点击!</font> </div> </div> </div> <div class="row needKno"> <div>文件导入说明:</div> <div>1、每次导入数据不超过Excel 1000行,文件大小不超过10M。</div> <div>2、数据录入完成后,直接保存为 “.xls” 格式导入。<font color="red">如果格式为“.xlsx”需另存为“.xls”格式</font></div> <div>3、请确保导入的数据正确。数据格式和模板一致。</div> <div><font color="red">注意:不要修改模板中每列前后的顺序,以及修改标题行的字段!</font></div> </div> </div> </body> [#include "/include/js.ftl" /] <script type="text/javascript"> //最新版layui 导入文件 /*upload.render({ elem: '#fileToUpload' ,url: '${base}/cargo_in/cargoInApply/importDetail.do?cargoInApplyId=${cargoInApplyId}' ,auto: false ,size: 10240 ,bindAction: '#fileToUploadSub' ,done: function(res){ console.log(res) } });*/ /*iframe 加载完成事件*/ var oFrm = document.getElementById("ifm"); oFrm.onload = oFrm.onreadystatechange = function() { if (this.readyState && this.readyState != 'complete'){ return; } else { onComplete();//加载完成后触发 } } //加载完成后触发 function onComplete(){ $("#uploadOk").css("display",""); //按钮恢复可点击状态 $("#inUpload").css("display","none"); var type=$(window.frames["ifm"].document).find("input[id='type']").val(); var content=$(window.frames["ifm"].document).find("input[id='content']").val(); var cargoInApplyId =$(window.frames["ifm"].document).find("input[id='cargoInApplyId']").val(); if(type == 'success'){ layer.confirm('导入入货明细成功,请选择接下来的操作', {icon:0, title:'是否选择流向', btn: ['查看明细','继续导入'] //按钮 }, function(index){ parent.layer.open({ type: 2, title:'<i class="fa fa-cog"></i>货物明细', skin: 'layui-layer-rim', //加上边框 scrollbar:false, area: ['80%', '80%'], //宽高 content: $base+"/cargo_in/cargoInApply/detailList.do?applyId="+cargoInApplyId }); }, function(){ parent.layer.closeAll(); parent.layer.open({ type: 2, title:'<i class="fa fa-cog"></i>导入入货明细', skin: 'layui-layer-rim', //加上边框 scrollbar:false, area: ['50%', '50%'], //宽高 content: $base+"/cargo_in/cargoInApply/importDetail.do?cargoInApplyId="+cargoInApplyId }); }); }else if(type == 'error'){ layer.alert(content, {icon: 7}); }else{ layer.alert('服务繁忙,请刷新页面重试!', {icon: 7}); } } //导入 function uploadOk(){ var fileToUpload=$("#fileToUpload").val(); if(fileToUpload==""||fileToUpload.length<5){ $("#option").html("请选择后缀名为 .xls的Excel文件!"); }else{ var suffix=fileToUpload.substring(fileToUpload.length-4,fileToUpload.length); if(suffix!=".xls"){ $("#option").html("请选择后缀名为 .xls的Excel文件!"); }else if($("#option2").html()!=''){ }else{ $("#uplodFileForm")[0].submit(); $("#uploadOk").css("display","none");//导入按钮不可点击状态 $("#inUpload").css("display",""); } } } //导入文件大小提示 function onUploadFileChange() { var fileInput=document.getElementById("fileToUpload"); if(fileInput.files[0].size>(10*1024*1024)){ $("#option2").html("导入文件大小不能超过10M !"); } } //关闭提示 function closeOption(){ $("#option").html(""); $("#option2").html(""); $("#successDiv").css("display","none"); $("#errorDiv").css("display","none"); $("#errorDownloadDiv").css("display","none"); $("#errorOption").css("display","none"); } </script> </html>
java处理代码:
@RequestMapping(value = "/importDetail", method = RequestMethod.POST) public String importDetail(@RequestParam(value = "fileToUpload", required = false)MultipartFile fileToUpload, String cargoInApplyId, ModelMap model, HttpServletRequest request, HttpServletResponse response, HttpSession session){ CargoInApply apply = cargoInApplyService.selectApplyByApplyId(cargoInApplyId); Workbook workbook = null; try { workbook = Workbook.getWorkbook(fileToUpload.getInputStream()); List<CargoInApplyDetail> custLists = new ArrayList<CargoInApplyDetail>();//存放正确数据的实体集合 CargoInApplyDetail detail; int colCount = workbook.getSheets()[0].getColumns(); if (colCount < 8){ model.addAttribute("type", "error"); return "/bulkxl/inCargoInApply/import"; } else { for (Sheet sheet : workbook.getSheets()){ Integer rowCount = sheet.getRows(); for (int i = 1; i < rowCount; i++) { //判断excel 一行是否为空数据 if(cellIsEmpty(sheet,2,i)){ continue; } //数据正确的话 detail = new CargoInApplyDetail(); //将单元格的数据保存到实体类中 detail.setBrand(sheet.getCell(0, i).getContents().trim()); detail.setMarkHead(sheet.getCell(1, i).getContents().trim()); custLists.add(detail); } } if(custLists == null || custLists.size() == 0){ model.addAttribute("content", "入货明细为空,导入失败"); model.addAttribute("type", "error"); }else{ //保存数据 boolean isOk = cargoInApplyDetailService.insertBatch(custLists); if(isOk && (apply.getCheckStatus() == 1 || apply.getCheckStatus() == 2)){ model.addAttribute("content", "导入入货明细成功,请核实数据是否正确,如有数据错误,请手动添加"); model.addAttribute("type", "success"); model.addAttribute("cargoInApplyId", apply.getCargoInApplyId()); }else{ model.addAttribute("content", "导入入货明细失败"); model.addAttribute("type", "error"); } } } } catch (Exception e) { e.printStackTrace(); model.addAttribute("content", e.getMessage()); model.addAttribute("type", "error"); } return "/bulkxl/inCargoInApply/import"; } /** * @Title: cellIsEmpty * @Description: 判断excel一行是否为空数据 * @param @param sheet * @param @param col * @param @param row * @param @return * @return Boolean * @throws * @author * @date 2016年11月2日 下午2:07:16 */ public Boolean cellIsEmpty(Sheet sheet,int col,int row){ for(int i = 0;i<col;i++){ String cell=sheet.getCell(i, row).getContents().trim(); if(cell != null && !"".equals(cell)){ return false; } } return true; }
里面牵扯着部分业务处理,请无视。