java学习(四) excel读取

private static void readExcel() {
        String filePath = "C:/Standardzid.xls";
        File file = new File(filePath);
        List<String> citys = new ArrayList<String>();
        List<LandPropertyType> cityList = null;
        List<List<LandPropertyType>> propertys = null;
        try {
            POIFSFileSystem readPoiFileSystem = new POIFSFileSystem(new FileInputStream(file));
            HSSFWorkbook workbook = new HSSFWorkbook(readPoiFileSystem);
            HSSFSheet sheet = workbook.getSheetAt(0);
            boolean flag = true;
            for (Row row : sheet) {
                if (flag) {
                    for (Cell cell : row) {
                        String cityName = cell.getStringCellValue();
                        if (cityName != null && cityName.length() > 0) {
                            citys.add(cityName);
                        }
                        propertys = new ArrayList<List<LandPropertyType>>();
                        for (int i = 0; i < citys.size(); i++) {
                            cityList = new ArrayList<LandPropertyType>();
                            propertys.add(cityList);
                        }
                    }
                    flag = false;
                } else {
                    String propertyType = row.getCell(0).getStringCellValue();
                    int lastCell = row.getLastCellNum();
                    for (int i = 2; i < lastCell; i++) {
                        Cell cell=row.getCell(i);
                        if(cell!=null){
                            String propertyName = row.getCell(i).getStringCellValue();
                            if (propertyName != null && propertyName.length() > 0) {
                                String[] propertyNames = propertyName.split(",");
                                for (String name : propertyNames) {
                                    LandPropertyType landPropertyType = new LandPropertyType(name,LandProperty.valueOf(propertyType));
                                    propertys.get(i - 2).add(landPropertyType);
                                }
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < citys.size(); i++) {
                String city = citys.get(i);
                LandPropertyType[] newLandPropertys = new LandPropertyType[propertys.size()];
                propertyMap.put(city, propertys.get(i).toArray(newLandPropertys));
            }
            
} catch (Exception e) { e.printStackTrace(); } }
原文地址:https://www.cnblogs.com/ry123/p/3863420.html