Пример #1
0
 // 老的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;
 }
Пример #2
0
  // 统一的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;
  }