Java 使用Apache POI 从Excel 文件中获取列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2922692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:32:23  来源:igfitidea点击:

to get columns from Excel files using Apache POI?

javaapacheexcelapache-poi

提问by posdef

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 find anything about getting columns neither in the API (link text) nor through google searching.

为了进行一些统计分析,我需要在 Excel 工作表的一列中提取值。我一直在使用 Apache POI 包来读取 Excel 文件,当需要遍历行时它工作正常。但是,无论是在 API(链接文本)还是通过谷歌搜索,我都找不到任何关于获取列的信息。

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,

谢谢,

采纳答案by Gagravarr

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.

Excel 文件是基于行而不是基于列的,因此获取列中所有值的唯一方法是依次查看每一行。没有更快的方法来获取列,因为列中的单元格没有存储在一起。

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.

然后会为您提供该列中的所有数字单元格值。

回答by qwerty

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

                }    
            }