2014-09-04 42 views
-2

我还是使用POI新建的,但是我想简单地在Excel工作簿中迭代单个列。例如,在下面的表格中,如果我只想要列A的值(列0中的行0-3),是否有一种有效的方法来执行此操作?我所见过的所有POI都是非常基于行的。Apache POI - 迭代单列下一页

____A__________B______ 
1| some | some | 
2| sample | other | 
3| text | stuff | 
4| here | here | 
______________________ 

我有几个电子表格,可以是大的,我顺利通过全片迭代,但我只是想弄清楚处理这样的事情最好的做法。

在此先感谢。

+1

你有什么已经尝试过,什么是不工作?请提供一个SSCCE。 – Opal 2014-09-04 19:56:45

回答

2

不幸的是,在Apache POI中没有列概念,因为从Excel继承的对象模型,所以你必须遍历行。

Here可能与示例重复。

+0

非常感谢你。 – pinkdevelops 2014-09-04 20:57:20

0

这里是你如何能做到这

for(Row r : datatypeSheet) 
      { 
       Iterator<Cell> headerIterator = r.cellIterator(); 
       Cell header = null; 
// table header row 
       if(r.getRowNum() == 0) 
       { 
        // getting specific column's index 

        while(headerIterator.hasNext()) 
        { 
         header = headerIterator.next(); 
         if(header.getStringCellValue().equalsIgnoreCase("A")) 
         { 
          AIndex = header.getColumnIndex(); 
         } 
         else if(header.getStringCellValue().equalsIgnoreCase("B")) 
         { 
          BIndex = header.getColumnIndex(); 
         } 
        } 
       } 
       else 
       { 
        Cell ACells = r.getCell(AIndex); 
        Cell BCells = r.getCell(BIndex); 
        if(ACells != null) 
        { 
         if(ACells.getCellType() == Cell.CELL_TYPE_STRING) 
         { 
          AData.add(ACells.getStringCellValue()); //arraylist storing A's data 
         } 
        } 
        if(BCells != null) 
        { 
         if(BCells.getCellType() == Cell.CELL_TYPE_STRING) 
         { 
          BData.add(BCells.getStringCellValue()); //arraylist storing B's data 
         } 
        } 
       } 
      }