0

I'm still new with POI, but I want to simply iterate down a single column in an Excel Workbook. For instance in the table below if I just want the values of column A, which would be the rows 0-3 in column 0, is there an efficient way to do this? Everything with POI I have seen has been very row-based.

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

I have a few spreadsheets that can be large, and I successfully can iterate through the whole sheet, but I'm just trying to figure out the best practice for handling something like this.

Thanks in advance.

pinkdevelops
  • 718
  • 3
  • 7
  • 16

2 Answers2

2

Unfortunately there is not column concept in apache POI because of the object model inherited from Excel so you have to iterate over the rows.

Here a possible duplicate with an example.

Community
  • 1
  • 1
0

Here's how you can do it

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
                        }
                    }
                }
            }
qwerty
  • 2,392
  • 3
  • 30
  • 55