码迷,mamicode.com
首页 > 编程语言 > 详细

Java poi导入Excel

时间:2020-06-10 11:17:24      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:ber   original   tle   common   rop   ima   color   last   list   

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

标签:ber   original   tle   common   rop   ima   color   last   list   

原文地址:https://www.cnblogs.com/yyjspace/p/13083462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!