Java poi导入Excel
Posted 艺洁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java poi导入Excel相关的知识,希望对你有一定的参考价值。
public MessageTo importPartyMember(MultipartFile file) { try { InputStream is = file.getInputStream(); Workbook hssfWorkbook = null; if (file.getOriginalFilename().endsWith("xlsx")){ hssfWorkbook = new XSSFWorkbook(is);//Excel 2007 }else if (file.getOriginalFilename().endsWith("xls")){ hssfWorkbook = new HSSFWorkbook(is);//Excel 2003 } Sheet hssfSheet = hssfWorkbook.getSheetAt(0); String titleformat = "姓名,性别,出生年月,身份证,学历,电子邮箱,入党时间,所属党组织,性质,"; String title = ""; Row firstRow = hssfSheet.getRow(0); for (int j = 0; j < firstRow.getLastCellNum(); j++) { title +=firstRow.getCell(j).toString().trim()+ ‘,‘; } if (!title.equals(titleformat)) { return MessageTo.ofError("文件不符合格式!你可以下载模板重新导入!"); } List<String> nameList=new ArrayList<>(); for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { Row hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null){ if(null==hssfRow.getCell(0)){ return MessageTo.ofError("Excel姓名有列为空"); } if(null==hssfRow.getCell(6)){ return MessageTo.ofError("Excel入党时间有列为空"); } if(null==hssfRow.getCell(7)){ return MessageTo.ofError("Excel所属党组织有列为空"); } nameList.add(hssfRow.getCell(7).toString()); } } List<CommonPartyOrgan> partyOrganList=commonPartyOrganMapperExt.getPartyOrganByName(nameList); for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { Row hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null) { String partyName=hssfRow.getCell(7).toString(); List<CommonPartyOrgan> partyOrgan1= partyOrganList.stream().filter( (CommonPartyOrgan s) ->s.getName().equals(partyName)) .collect(Collectors.toList()); if(partyOrgan1.size()>0) { CommonPartyMember partyMember=new CommonPartyMember(); partyMember.setId(PrimaryKeyUtil.getPrimaryKeyId()); partyMember.setName(hssfRow.getCell(0).toString()); partyMember.setSex(hssfRow.getCell(1)==null?null:hssfRow.getCell(1).toString()); partyMember.setBirthday(hssfRow.getCell(2)==null?null:hssfRow.getCell(2).getDateCellValue()); partyMember.setIdNumber(hssfRow.getCell(3)==null?null:hssfRow.getCell(3).toString()); partyMember.setEducation(hssfRow.getCell(4)==null?null:hssfRow.getCell(4).toString()); partyMember.setEmail(hssfRow.getCell(5)==null?null:hssfRow.getCell(5).toString()); partyMember.setPartyTime(hssfRow.getCell(6).getDateCellValue()); partyMember.setPartyOrganId(partyOrgan1.get(0).getId()); String type=hssfRow.getCell(8)==null?null:hssfRow.getCell(8).toString(); Byte MemberProperties=null; if(type.equals("正式党员")) MemberProperties=1; else if(type.equals("预备党员")) MemberProperties=2; else if(type.equals("发展党员")) MemberProperties=3; else if(type.equals("入党积极分子")) MemberProperties=3; partyMember.setMemberProperties(MemberProperties); partyMember.setCreateUserId(LoginContext.getUserId()); commonPartyMemberMapper.insertSelective(partyMember); } else { return MessageTo.ofError(partyName+"不存在"); } } } return MessageTo.ofSuccess("0","导入成功"); } catch (Exception e) { e.printStackTrace(); return MessageTo.ofError("500",e.getMessage()); } }
以上是关于Java poi导入Excel的主要内容,如果未能解决你的问题,请参考以下文章
Java poi导入excel(合并单元格的excel)(转)
java poi 导入 excel怎么设置单元格格式为 文字