to get columns from Excel files using Apache POI?

后端 未结 2 1318
深忆病人
深忆病人 2020-12-14 20:28

In order to do some statistical analysis I need to extract values in a column of an Excel sheet. I have been using the Apache POI package to read from Excel files, and it wo

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 20:38

    Just wanted to add, in case you have headers in your file and you are not sure about the column index but want to pick columns under specific headers (column names) for eg, you can try something like this

        for(Row r : datatypeSheet) 
                {
                    Iterator 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("column1Index"))
                            {
                                column1Index = header.getColumnIndex();
                            }
                        }
                    }
                    else
                    {
                        Cell column1Cells = r.getCell(column1);
    
                        if(column1Cells != null) 
                        {
                            if(column1Cells.getCellType() == Cell.CELL_TYPE_NUMERIC) 
                            {
    // adding to a list
                                column1Data.add(column1Cells.getNumericCellValue());
                            }
                            else if(column1Cells.getCellType() == Cell.CELL_TYPE_FORMULA && column1Cells.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) 
                            {
    // adding to a list
                                column1Data.add(column1Cells.getNumericCellValue());
                            }
                        }
    
                    }    
                }
    

提交回复
热议问题