xlsx2json2file

6736 단어 Javaxlsx2json
POI 를 사용 하여 xlsx 를 json 으로 변환 하여 파일 을 기록 합 니 다.
Maven 의존

      org.apache.poi
      poi
      3.14
    
    
      org.apache.poi
      poi-ooxml
      3.14
    

    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.3
    
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.3
    
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.9.3
    
public static void main(String[] args) throws IOException {
        Workbook wb = null;
        Sheet sheet = null;
        Row row = null;
        List> list = null;
        String cellData = null;
        String filePath = "F:\\workSpace\\IdeaProjects\\c_working\\asiainfo\\eda\\excel2json\\src\\main\\java\\a.xlsx";
        String[] columns = {"channel_name", "polygon_id", "point_order", "lng", "lat"};
        wb = readExcel(filePath);
        if (wb != null) {
            //        
            list = new ArrayList<>();
            //     sheet
            sheet = wb.getSheetAt(0);
            //      
            int rownum = sheet.getPhysicalNumberOfRows();
            //     
            row = sheet.getRow(0);
            //      
            int colnum = row.getPhysicalNumberOfCells();
            // String initName = sheet.getRow(1).getCell(0).getStringCellValue() + "-" + new Double(sheet.getRow(1).getCell(1).getNumericCellValue()).intValue() + "";
            String initName = "";
            ArrayList list3 = new ArrayList<>();
            ArrayList list4 = null;
            for (int i = 1; i < rownum; i++) {
                list4 = new ArrayList<>();
                row = sheet.getRow(i);
                String name = row.getCell(0).getStringCellValue() + "-" + new Double(row.getCell(1).getNumericCellValue()).intValue() + "";
                double lng = row.getCell(3).getNumericCellValue();
                double lat = row.getCell(4).getNumericCellValue();
                System.out.println(initName + "?" + name);
                list4.add(lng);
                list4.add(lat);

                if ("".equals(initName)) {
                    initName = name;
                }
                if (!initName.equals(name)) {
                    System.out.println("\r
"); // json Map fmap = new HashMap<>(16); ArrayList features = new ArrayList<>(); fmap.put("type", "FeatureCollection"); Map propertiesMap = new HashMap<>(16); propertiesMap.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84"); Map crsMap = new HashMap<>(16); crsMap.put("type", "name"); crsMap.put("properties", propertiesMap); fmap.put("crs", crsMap); Map featureMap = new HashMap<>(16); featureMap.put("type", "Feature"); Map fpMap = new HashMap<>(1); Map geometryMap = new HashMap<>(16); geometryMap.put("type", "MultiPolygon"); fmap.put("name", initName); fpMap.put("NAME", initName); featureMap.put("properties", fpMap); ArrayList list1 = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); list3.add(list3.get(0)); list2.add(list3); list1.add(list2); geometryMap.put("coordinates", list1); featureMap.put("geometry", geometryMap); System.out.println(" "); features.add(featureMap); fmap.put("features", features); ObjectMapper mapper = new ObjectMapper(); String s = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(fmap); System.out.println(initName + ":" + list3.size()); list3.clear(); list3.add(list4); File file = new File(initName + ".geojson"); if (!file.exists()) { file.createNewFile(); } // true, append file FileWriter fileWritter = new FileWriter(file.getName(), false); fileWritter.write(s); fileWritter.close(); initName = name; }else{ list3.add(list4); } } } } // excel public static Workbook readExcel(String filePath) { Workbook wb = null; if (filePath == null) { return null; } String extString = filePath.substring(filePath.lastIndexOf(".")); InputStream is = null; try { is = new FileInputStream(filePath); if (".xls".equals(extString)) { return wb = new HSSFWorkbook(is); } else if (".xlsx".equals(extString)) { return wb = new XSSFWorkbook(is); } else { return wb = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return wb; }

좋은 웹페이지 즐겨찾기