摆脱使用Apache POI Excel文件列? [英] to get columns from Excel files using Apache POI?

查看:194
本文介绍了摆脱使用Apache POI Excel文件列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了做一些统计分析,我需要在Excel工作表中的列来提取值。我一直在使用Apache POI包从Excel文件读取,并且当需要遍历排它工作正常。但是我无法找到有关获取列既不在API(链接文本),也通过谷歌搜索什么。

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.

您code可能想是这样的:

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.

这篇关于摆脱使用Apache POI Excel文件列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆