Java poi导入Excel

 public MessageTo insertExcel(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> regionnameList = new ArrayList<>();
            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(1)) {
                        return MessageTo.ofError("Excel类型有列为空");
                    }
                    if (null == hssfRow.getCell(3)) {
                        return MessageTo.ofError("Excel所属市有列为空");
                    }
                    if (null == hssfRow.getCell(4)) {
                        return MessageTo.ofError("Excel所属地区类型有列为空");
                    }
                    nameList.add(hssfRow.getCell(0).toString());
                    regionnameList.add(hssfRow.getCell(3).toString());
                    regionnameList.add(hssfRow.getCell(4).toString());
                }
            }
            regionnameList = regionnameList.stream().distinct().collect(Collectors.toList());
            List<CommonRegion> regionList = commonRegionMapperExt.getRegionByName(regionnameList);
            List<CommonPartyOrgan> partyOrganList = commonPartyOrganMapperExt.getPartyOrganByName(nameList);
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                Row hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
                    String name = hssfRow.getCell(0).toString();
                    List<CommonPartyOrgan> partyOrgan1 = partyOrganList.stream().filter(
                            (CommonPartyOrgan s) -> s.getName().equals(name))
                            .collect(Collectors.toList());
                    if (partyOrgan1.size() == 0) {
                        CommonPartyOrgan partyOrgan = new CommonPartyOrgan();
                        partyOrgan.setId(PrimaryKeyUtil.getPrimaryKeyId());
                        partyOrgan.setName(name);
                        String type = hssfRow.getCell(1).toString();
                        Byte OrganType = null;
                        if (type.equals("党委"))
                            OrganType = 1;
                        else if (type.equals("党总支"))
                            OrganType = 2;
                        else if (type.equals("党支部"))
                            OrganType = 3;
                        partyOrgan.setOrganType(OrganType);
                        partyOrgan.setCode(hssfRow.getCell(2).toString());
                        String city = hssfRow.getCell(3).toString();
                        List<CommonRegion> list1 = regionList.stream().filter((CommonRegion s) -> s.getRegionName().equals(city))
                                .collect(Collectors.toList());
                        if (list1.size() > 0) {
                            partyOrgan.setProvinceCode(list1.get(0).getParentRegionCode());
                            partyOrgan.setCityCode(list1.get(0).getRegionCode());
                        } else
                            return MessageTo.ofError(city + "不存在");
                        String area = hssfRow.getCell(4).toString();
                        List<CommonRegion> list2 = regionList.stream().filter((CommonRegion s) -> s.getRegionName().equals(area))
                                .collect(Collectors.toList());
                        if (list2.size() > 0)
                            partyOrgan.setAreaCode(list2.get(0).getRegionCode());
                        else
                            return MessageTo.ofError(area + "不存在");
                        if (hssfRow.getCell(5) != null)
                            partyOrgan.setLongitude(Double.valueOf(hssfRow.getCell(5).toString()));
                        if (hssfRow.getCell(6) != null)
                            partyOrgan.setLatitude(Double.valueOf(hssfRow.getCell(6).toString()));
                        partyOrgan.setAddress(hssfRow.getCell(7) == null ? null : hssfRow.getCell(7).toString());
                        String tel = "";
                       int cellType = hssfRow.getCell(8).getCellType();
                       if (cellType == 0) {
                           DataFormatter dataFormatter = new DataFormatter();
                          dataFormatter.addFormat("###########", null);
                           tel = dataFormatter.formatCellValue(hssfRow.getCell(8));
                        } else {
                           tel = hssfRow.getCell(8).toString();
                       }
                        Date time = hssfRow.getCell(9) == null ? null : hssfRow.getCell(9).getDateCellValue();
                        partyOrgan.setEstablishedTime(time);
                        partyOrgan.setCreateUserId(LoginContext.getUserId());
                        Date now = new Date();
                        partyOrgan.setCreateTime(now);
                        partyOrgan.setIsdel("N");
                        commonPartyOrganMapper.insertSelective(partyOrgan);
                    }
                }
            }
            return MessageTo.ofSuccess("0", "导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return MessageTo.ofError("500", e.getMessage());
        }
    }

 

posted @ 2020-06-10 10:25  艺洁  阅读(251)  评论(0编辑  收藏  举报