Konvertierung von excel -, JSON in JAVA-Verwendung von poi und jackson

Ich versuche zum konvertieren einer excel -, Json-und sollte es speichern in einem lokalen Verzeichnis. Ich bin in der Lage, speichern Sie die Json-Datei, aber Alle sind immer Null-Werte. Es ist das Lesen von Excel-Zeile für Zeile-und da sind sich, verändert sich in das JSON-format und speichern es wieder geht zur nächsten Zeile und ersetzen die bisherigen Json-Datei mit den aktuellen Zeilendaten. Es ist in while-Schleife so ist es überarbeiten und ersetzen Sie die Datei. Wenn ich den converter code aus, während nur eine Zeile von Daten kommt, die null-Werte mit den Spaltennamen (json-Daten) kommt eher als 34 Zeile Daten(vollständige Daten).

Hier ist der code. Bitte empfehlen Sie mir, wie kann ich erreichen, das gesamte excel-Daten konvertiert werden und sollten speichern Sie die Datei im lokalen Verzeichnis.

public static void uploadXLS(MultipartFile file, Document doc)
        throws IOException {

    Products products = new Products();

    List<Products> productsList = new ArrayList<Products>();

    logger.info("uploadExcel method");
    HSSFWorkbook wb = null;
    try {

         wb= new HSSFWorkbook(file.getInputStream());
            System.out.println("workbook: "+wb);
            HSSFSheet sheet = wb.getSheetAt(0);
            System.out.println("worksheet: "+sheet);
            HSSFRow row;

            Iterator<Row> iterator = sheet.iterator();

            while (iterator.hasNext()) {
            products = new Products();
                Row nextRow = iterator.next();
                Iterator<Cell> cellIterator = nextRow.cellIterator();


                    Cell cell = cellIterator.next();

                    Iterator cells = nextRow.cellIterator();

                        cell=(HSSFCell) cells.next();

                        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
                        {
                            System.out.print(cell.getStringCellValue()+" ");
                        }
                        else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
                        {
                            System.out.print(cell.getNumericCellValue()+" ");
                        }
                        else if(HSSFDateUtil.isCellDateFormatted(cell)){
                            Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
                        } else
                        {
                            //U Can Handel Boolean, Formula, Errors
                        }

                        products.setId(new DataFormatter().formatCellValue(nextRow.getCell(0)));
                        products.setProductId(new DataFormatter().formatCellValue(nextRow.getCell(1)));
                        products.setPopularity((new DataFormatter().formatCellValue(nextRow.getCell(12))));
                        products.setRelevance((new DataFormatter().formatCellValue(nextRow.getCell(13))));
                        products.setShortlisted((new DataFormatter().formatCellValue(nextRow.getCell(14))));
                        products.setLikes((new DataFormatter().formatCellValue(nextRow.getCell(15))));
                        products.setCreateDt((new DataFormatter().formatCellValue(nextRow.getCell(16))));
                        products.setPageId((new DataFormatter().formatCellValue(nextRow.getCell(17))));
                        products.setStyleName(nextRow.getCell(18).getStringCellValue());
                        products.setStyleId(nextRow.getCell(19).getStringCellValue());
                        products.setPriceRange(nextRow.getCell(20).getStringCellValue());
                        products.setPriceId(nextRow.getCell(21).getStringCellValue());
//                         products.setDefaultPrice(nextRow.getCell(22).getStringCellValue());
                             products.setDefaultMaterial(nextRow.getCell(23).getStringCellValue());
                        products.setDefaultFinish((new DataFormatter().formatCellValue(nextRow.getCell(24))));


                    productsList.add(products);
                    System.out.println(productsList.add(products));



           //JSON CONVERTER
    ObjectMapper mapper = new ObjectMapper();

    System.out.println("productsList: "+products);
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
      Date date = new Date();
      String location = dateFormat.format(date);
      System.out.println("productsList final: "+products);



        //Convert object to JSON string
        String jsonInString = mapper.writeValueAsString(products);
        System.out.println("JsonInString " +jsonInString);

        //Convert object to JSON string and pretty print
        jsonInString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(products);

       mapper.writeValue(new File("D:\\"+location+"products.json"), productsList);

            }
            } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {


    }
}

Bin anbringen der Bildschirm gedreht, hier kann man sehen, dass es 34 Zeilen in excel. Es tut gut und die Anzeige der Werte, sondern die letzten Json-Datei generiert haben, die null-Werte. Alle 34 Zeilen Daten mit null-Werten 🙁
Konvertierung von excel -, JSON in JAVA-Verwendung von poi und jackson

Danke viel im Voraus :). In der Hoffnung, dass irgendjemand kann mir aus diesem Problem.

InformationsquelleAutor | 2016-03-03

Schreibe einen Kommentar