开发者

How to get columns from Excel files using Apache POI?

开发者 https://www.devze.com 2023-01-01 11:44 出处:网络
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 works fine when one needs to

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 works fine when one needs to iterate over rows. However I couldn't fin开发者_开发百科d anything about getting columns neither in the API (link text) nor through google searching.

As I need to get max and min values of different columns and generate random numbers using these values, so without picking up individual columns, the only other option is to iterate over rows and columns to get the values and compare one by one, which doesn't sound all that time-efficient.

Any ideas on how to tackle this problem?

Thanks,


Excel files are row based rather than column based, so the only way to get all the values in a column is to look at each row in turn. There's no quicker way to get at the columns, because cells in a column aren't stored together.

Your code probably wants to be something like:

List<Double> values = new ArrayList<Double>();
for(Row r : sheet) {
   Cell c = r.getCell(columnNumber);
   if(c != null) {
      if(c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
         valuesadd(c.getNumericCellValue());
      } else if(c.getCellType() == Cell.CELL_TYPE_FORMULA && c.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) {
         valuesadd(c.getNumericCellValue());
      }
   }
}

That'll then give you all the numeric cell values in that column.


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<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("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());
                        }
                    }

                }    
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消