2016-06-11 122 views
-2

我從excel中獲取數據。並存儲在列表中。然後 我想在List 我的問題在主要方法第一個循環它需要一個列表詳細信息中的單個元素。在第二個循環中,我想要列表 中的一個元素作爲示例[OS1, 10.23.14.25, 12.0, wradadasda]我想要OS1如何獲取它。 在我的代碼second loop也顯示整個數組。獲取列表中的單個元素

public static List readDataFromExcel() throws IOException{ 
     String filename = "path"; 

     List sheetData = new ArrayList(); 

     FileInputStream fis = null; 
     try { 

      fis = new FileInputStream(filename); 

      XSSFWorkbook workbook = new XSSFWorkbook(fis); 
      XSSFSheet sheet = workbook.getSheetAt(0); 

      Iterator rows = sheet.rowIterator(); 
      while (rows.hasNext()) { 
       XSSFRow row = (XSSFRow) rows.next(); 
       Iterator cells = row.cellIterator(); 

       List data = new ArrayList(); 
       while (cells.hasNext()) { 
        XSSFCell cell = (XSSFCell) cells.next(); 
        String value=" "; 
        switch (cell.getCellType()) 
        { 
         case Cell.CELL_TYPE_NUMERIC: 
          value = BigDecimal.valueOf(cell.getNumericCellValue()).toPlainString(); 
          data.add(value); 
          break; 
         case Cell.CELL_TYPE_STRING: 
          value=cell.getStringCellValue(); 
          data.add(value); 
          break; 
         case Cell.CELL_TYPE_BLANK: 
          value = " ".toString(); 
          data.add(value); 
          break; 
         case Cell.CELL_TYPE_BOOLEAN: 
          value = Boolean.valueOf(cell.getBooleanCellValue()).toString(); 
          data.add(value); 
          break; 
        } 

       } 
       sheetData.add(data); 
       fis.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    return sheetData; 
    } 

主要方法

public static void main(String[]args) throws IOException{ 

     List getdatafromexcel = readDataFromExcel(); 
     List oneserverdetailsList =null; 
     for(int i =0;i<getdatafromexcel.size();i++){ 
      oneserverdetailsList = new ArrayList(); 
      oneserverdetailsList.add(getdatafromexcel.get(i)); 
      for(int j=0;j<oneserverdetailsList.size();j++){ 
       System.out.println(oneserverdetailsList.get(0)); 
      } 

     } 

出把圖像 -

Excel文件數據 -

+2

你張貼了大量的代碼和解釋,但我真的不要得到你正在尋找的東西。你能準確地發佈,給定的輸入,你期望它打印出來嗎?與此相反,你的代碼產生了什麼。 – thst

+0

主要方法中的問題。在第一個循環中,它需要一個List細節。在第二個循環中,我需要該List的一個元素。例如在第一個循環中,需要[OS1,10.2.14.14.25,12.0,wradadasda]第二個循環我想要OS1 – KH19

回答

1

你的問題不是很清楚我假設,並回答你的問題。

要添加列表,其他列表中,所以當你打電話

getdatafromexcel.get(i) // This will return a list 

因此,要訪問元素試試這個

oneserverdetailsList.get(0).get(0) //To access OS1 assuming it is in first index. Explaination : First get will return you a list and second get will return you an element.