// 老的EasyUI上传方式(2013/05/28废弃) @RequestMapping(params = "implXls") @ResponseBody public AjaxJson implXls(HttpServletRequest request, HttpServletResponse response) { AjaxJson j = new AjaxJson(); MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request; MultipartFile file = mulRequest.getFile("filedata"); List<JpPersonEntity> listPersons; try { boolean isSuccess = true; listPersons = ExcelImportUtil.importExcel( file.getInputStream(), JpPersonEntity.class, new ImportParams()); for (JpPersonEntity person : listPersons) { person.setId(UUIDGenerator.generate()); if (person.getAge() == null || person.getCreatedt() == null || person.getSalary() == null) { isSuccess = false; break; } else { jpPersonService.save(person); } } if (isSuccess) j.setMsg("文件导入成功!"); else j.setMsg("文件格式不正确,导入失败!"); } catch (IOException e) { j.setMsg("文件导入失败!"); logger.error(ExceptionUtil.getExceptionMessage(e)); } catch (Exception e) { j.setMsg("文件格式不正确,导入失败!"); } return j; }
// 统一的Excel上传导入方式 @RequestMapping(params = "importExcel", method = RequestMethod.POST) @ResponseBody public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) { AjaxJson j = new AjaxJson(); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile file = entity.getValue(); // 获取上传文件对象 List<JpPersonEntity> listPersons; try { listPersons = ExcelImportUtil.importExcel( file.getInputStream(), JpPersonEntity.class, new ImportParams()); for (JpPersonEntity person : listPersons) { if (person.getAge() != null) { person.setId(UUIDGenerator.generate()); jpPersonService.save(person); } } j.setMsg("文件导入成功!"); } catch (Exception e) { j.setMsg("文件导入失败!"); logger.error(ExceptionUtil.getExceptionMessage(e)); } // break; // 不支持多个文件导入? } return j; }
/** * Excel导出列表页面跳转 * * @return */ @RequestMapping(params = "addorupdate") public ModelAndView addorupdate(JpPersonEntity jpPerson, HttpServletRequest req) { if (StringUtil.isNotEmpty(jpPerson.getId())) { jpPerson = jpPersonService.getEntity(JpPersonEntity.class, jpPerson.getId()); req.setAttribute("jpPersonPage", jpPerson); } return new ModelAndView("jeecg/demo/test/jpPerson"); }
/** * 删除Excel导出 * * @return */ @RequestMapping(params = "del") @ResponseBody public AjaxJson del(JpPersonEntity jpPerson, HttpServletRequest request) { AjaxJson j = new AjaxJson(); jpPerson = systemService.getEntity(JpPersonEntity.class, jpPerson.getId()); message = "删除成功"; jpPersonService.delete(jpPerson); systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO); j.setMsg(message); return j; }
/** * 添加Excel导出 * * @param ids * @return */ @RequestMapping(params = "save") @ResponseBody public AjaxJson save(JpPersonEntity jpPerson, HttpServletRequest request) { AjaxJson j = new AjaxJson(); if (StringUtil.isNotEmpty(jpPerson.getId())) { message = "更新成功"; jpPersonService.saveOrUpdate(jpPerson); systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO); } else { message = "添加成功"; jpPersonService.save(jpPerson); systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO); } return j; }