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

问题描述:

为了进行一些统计分析,我需要在 Excel 工作表的一列中提取值.我一直在使用 Apache POI 包来读取 Excel 文件,当需要迭代行时它工作正常.但是我在 API (link text) 和通过谷歌搜索.

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.

由于我需要获取不同列的最大值和最小值并使用这些值生成随机数,因此无需选取单个列,唯一的其他选择是遍历行和列以获取值并进行一一比较,这听起来并不那么省时.

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?

谢谢,

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

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.