ApachePOI解析excel

 @Action(value = "region_importData",results = {@Result(name = "success",type = "json")})
    public String importData(){
        Map map = new HashMap();
        try {
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(upload));
            HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
            ArrayList list = new ArrayList<>();
            for (Row row :
                    sheetAt) {
                if (row.getRowNum() == 0){
                    continue;
                }
                if (StringUtils.isNotBlank(row.getCell(0).getStringCellValue())){
                    Region region = new Region();
                    String province = row.getCell(1).getStringCellValue();
                    String city = row.getCell(2).getStringCellValue();
                    String distrinct = row.getCell(3).getStringCellValue();
                    String[] headByString = PinYin4jUtils.getHeadByString(province + city + distrinct);
                    String shortCode = StringUtils.join(headByString);
                    region.setProvince(province);
                    region.setCity(city);
                    region.setDistrict(distrinct);
                    region.setPostcode(row.getCell(4).getStringCellValue());
                    region.setShortcode(shortCode);
                    region.setCitycode(PinYin4jUtils.hanziToPinyin(city));
                    list.add(region);
                }
            }
            regionService.saveRegions(list);
            map.put("resultss",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("resultss",false);
        }
        pushToValueStackRoot(map);
        return SUCCESS;
    }

你可能感兴趣的:(ApachePOI)