2014-05-03 24 views
0

我有一個Excel的格式如下如何從excel中匹配特定條件的值並將其分配給數組?

Category  Name     Title 
Map   Map1     Title Automaition Map1 
Topic  Topic1    Title Automaition Topic1 
Topic  Topic2    Title Automaition Topic2 
Submap  Submap1    Title Automaition Submap1 
Sub Topic SubTop1    Title Automaition SubTopic1 

我想借此從標題列中的值對所有類別的主題,並存儲在一個數組。我寫了下面的代碼,以從標題列中的值進行分類爲主題

public class DitaExcel { 

    @Test 
    public void Test() throws IOException { 
    { 
    FileInputStream file = new FileInputStream(new File("C://DitaExchange.xlsx")); 

      //Create Workbook instance holding reference to .xlsx file 
      XSSFWorkbook workbook = new XSSFWorkbook(file); 

      //Get first/desired sheet from the workbook 
      XSSFSheet sheet = workbook.getSheetAt(0); 


      for (Row row : sheet) { 
       for (Cell cell : row) { 
        CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); 

        switch (cell.getCellType()) { 
         case Cell.CELL_TYPE_STRING: 


          if (cell.getRichStringCellValue().getString().equalsIgnoreCase("Topic")) 
          { 
           System.out.println(row.getCell(2)); 

           break; 
          } 
         break; 



        }}}}}} 

我得到的值作爲 標題Automaition TOPIC1 標題Automaition TOPIC2

但我想裏面捕獲這些值數組。我如何捕獲數組中的這些值。我期待我的輸出是這樣

{標題Automaition TOPIC1,標題Automaition TOPIC2}

是否有人可以幫忙嗎?

感謝

回答

0

改變你的代碼是這樣的...

 

    List list = new ArrayList(); 

code.. 
.... 
... 



    if (cell.getRichStringCellValue().getString().equalsIgnoreCase("Topic")) 
    { 
     list.add(row.getCell(2)); 

     break; 
    } 

... 
... 

    System.out.print("\t" + list); 

+0

謝謝,我能夠保存在你的代碼的數組值。 – Naseem

相關問題