2016-07-05 75 views
0

我有一个行迭代器,它经历了Excel表单并获取了该行中的每个单元格值。我已经成功地将值逐行打印出来,但是现在我想将每个单元格存储在单独的变量中,以便我们可以将它们传递到应用程序模块,在那里它们将用作插入函数中的参数。如何在变量中存储单元格值?谢谢。这里是代码:在ADF中获取单元格数据到变量中

  try { 

      InputStream is = file.getInputStream(); 
      HSSFWorkbook workbook = new HSSFWorkbook(is); 
      HSSFSheet sheet = workbook.getSheetAt(0); 

      System.out.print("File is up and the size is " + file.getLength() + " bytes\n"); 


      Iterator<Row> rowIterator = sheet.iterator(); 
      if (rowIterator.hasNext()) 
       rowIterator.next(); 

      while (rowIterator.hasNext()) { 

       Row row = rowIterator.next(); 

       Iterator<Cell> cellIterator = row.cellIterator(); 

       while (cellIterator.hasNext()) { 
        Cell cell = cellIterator.next(); 
        //Check the cell type and format accordingly 
        switch (cell.getCellType()) { 
        case Cell.CELL_TYPE_NUMERIC: 
         System.out.print(cell.getNumericCellValue() + "\t"); 

         break; 
        case Cell.CELL_TYPE_STRING: 
         System.out.print(cell.getStringCellValue() + "\t"); 

         // System.out.print(cell.getNumericCellValue()+ "\t"); 
         break; 
        } 
       } 
       System.out.println(""); 

      } 
      // workbook.close(); 
      // file.close(); 

     } 

     catch (IOException e) { 
      System.out.print("greška"); 
     } 

回答

0

对于任何人想知道如何做到这一点。遍历每一行并将其存储在一个arrayList中,然后将每个ArrayList存储到另一个ArrayList中。它应该看起来像这个ArrayList>。获得该对象后,将其发送到应用程序模块,在该模块中使用增强的for循环“解压缩”您的ArrayList。之后,从数组中提取元素为变量并将它们插入到视图对象中。

相关问题