Excel导入

    技术2022-07-11  110

    代码 后端接口

    @Login @ResponseBody @RequestMapping(value="/imports",produces="text/html; charset=utf-8") public String ImportDevice(MultipartFile uploadFile,HttpServletResponse response, HttpSession session,HttpServletRequest request) { Map<String, String> map = new HashMap<String, String>(); InputStream is = null; Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\-\\s]?((((0?" +"[13578])|(1[02]))[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))" +"|(((0?[469])|(11))[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|" +"(0?2[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12" +"35679])|([13579][01345789]))[\\-\\-\\s]?((((0?[13578])|(1[02]))" +"[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))" +"[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\-\\s]?((0?[" +"1-9])|(1[0-9])|(2[0-8]))))))"); String judge = ""; int num =0; List<TjfxQzGnxpt> djsqies = new ArrayList<TjfxQzGnxpt>(); try { is = uploadFile.getInputStream(); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); int rowNum = sheet.getPhysicalNumberOfRows(); for(int i = 1; i < rowNum; i++) { num = i; HSSFRow row = sheet.getRow(i); TjfxQzGnxpt gnxpt = new TjfxQzGnxpt(); row.getCell( (short) 1).setCellType(1); String zcnr = row.getCell( (short) 1).getStringCellValue(); gnxpt.setPtmc(zcnr); row.getCell( (short) 2).setCellType(1); String sbxh = row.getCell( (short) 2).getStringCellValue(); gnxpt.setPttype(sbxh); row.getCell( (short) 3).setCellType(1); String sbsls = row.getCell( (short) 3).getStringCellValue(); gnxpt.setYyzt(sbsls); row.getCell( (short) 4).setCellType(1); String sbsl = row.getCell( (short) 4).getStringCellValue(); gnxpt.setHzf(sbsl); row.getCell( (short) 5).setCellType(1); String sbsl2 = row.getCell( (short) 5).getStringCellValue(); gnxpt.setYyztssqz(sbsl2); row.getCell( (short) 5).setCellType(1); String sbsl3 = row.getCell( (short) 5).getStringCellValue(); gnxpt.setCltime(sbsl3); Calendar c = Calendar.getInstance(); gnxpt.setProjectid(UUID.randomUUID().toString()); gnxpt.setAddtime(c.getTime()); djsqies.add(gnxpt); } }catch (Exception e) { e.printStackTrace(); return "保存失败,文件解析异常!第" + num + "行出现数字或其他特殊符号,请转换正常格式后重新导入!"; } //新增和添加记录 if(gnxptService.insertTjfxQzGnxpt(djsqies) && importlog(request, num+"", "tjfx_kjj_gxjsqy")){ return "上传成功 "; }else { return judge; } }

    导入记录

    public boolean importlog(HttpServletRequest request ,String importcount, String importtargetfrom) { User user = (User) request.getSession().getAttribute("SESSIONUSER"); TjfxImportLog importLog = new TjfxImportLog(); importLog.setImportuserid(user.getId()+"");//导入ID ManageUser mu = manageUserMapper.getManageUserByUserId(user.getId()); importLog.setImportgroup(mu.getGroupId()+""); importLog.setImportamount(importcount);//导入条目数 importLog.setImporttargetfrom(importtargetfrom);//目标表 importLog.setProjectid(UUID.randomUUID().toString());//导入唯一标识 Calendar c = Calendar.getInstance(); importLog.setImporttime(c.getTime());//导入结束时间 return logService.insertTjfxImportLog(importLog); }

    前端

    function uploadIPC(obj){ var index = layer.load(1); var uploadFile=document.getElementById("uploadFile").value; if(uploadFile==""){ layer.alert("请选择导入的文件!"); return false; } $.ajaxFileUpload({ url:'<%=request.getContextPath()%>/djsqy/imports', type : "post", secureuri : false, fileElementId : 'uploadFile', dataType : "text", success : function(result) { layer.close(index); layer.alert(result,function(){ location.reload();//刷新父页面 }); } }); }

    按钮 layui渲染的按钮

    <div class="layui-form" style="padding:30px;"> <div class="layui-input-inline"> <input type="text" placeholder="请上传文件" autocomplete="off" class="layui-input E109_file_int" /> <input type="file" multiple autocomplete="off" class="layui-input E109_file" id="uploadFile" name="uploadFile"/> </div> <label for="uploadFile" class="layui-btn layui-unload-btns">选择文件</label> <label class="layui-btn" id="uploadBtn" onclick="uploadIPC(this)">导入</label> <a href="<%=request.getContextPath() %>/attachment/sbqd.xls" style="color:red;float: right;margin-right: 50px;">模板文件下载</a> </div>
    Processed: 0.009, SQL: 9