// 老的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; }
public static void main(String[] args) { ImportParams params = new ImportParams(); params.setTitleRows(0); params.setHeadRows(1); long start = new Date().getTime(); List<deptEntity> list = ExcelImportUtil.importExcel(new File("c:/dept.xls"), deptEntity.class, params); System.out.println(list.size()); for (int i = 0; i < list.size(); i++) { deptEntity deptEntity = list.get(i); String Name = deptEntity.getName(); String valid = deptEntity.getValid(); System.out.printf(sql, String.format("%03d", i + 1), Name, valid); } }
@SuppressWarnings("unchecked") @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(); // 获取上传文件对象 ImportParams params = new ImportParams(); params.setTitleRows(2); params.setSecondTitleRows(1); params.setNeedSave(true); try { List<WeixinCmsSiteEntity> listWeixinCmsSiteEntitys = (List<WeixinCmsSiteEntity>) ExcelImportUtil.importExcelByIs( file.getInputStream(), WeixinCmsSiteEntity.class, params); for (WeixinCmsSiteEntity weixinCmsSite : listWeixinCmsSiteEntitys) { weixinCmsSiteService.save(weixinCmsSite); } j.setMsg("文件导入成功!"); } catch (Exception e) { j.setMsg("文件导入失败!"); logger.error(ExceptionUtil.getExceptionMessage(e)); } finally { try { file.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); } } } return j; }