java 使用java读取单列excel表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5551815/
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-10-30 11:37:52  来源:igfitidea点击:

Read a single column of excel sheet using java

javaexcelapache-poi

提问by Priyanka Thube

I have an excel sheet. I want to write a method which takes parameter as column number that to be read and return a array consist of all the data in that column.

我有一个excel表。我想编写一个方法,它将参数作为要读取的列号,并返回一个由该列中的所有数据组成的数组。

And then to place that column element in xml sheet. How can I write a method to do that?

然后将该列元素放置在 xml 表中。我怎样才能写一个方法来做到这一点?

回答by uncaught_exceptions

Use Apache POI. You can find example in their usage page.

使用 Apache POI。您可以在他们的使用页面中找到示例。

Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
    for (Cell cell : row) {
    // Here you might have to use the cell number to limit what you want.
        CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum());
        System.out.print(cellRef.formatAsString());
        System.out.print(" - ");

        switch(cell.getCellType()) {
      case Cell.CELL_TYPE_STRING:
        System.out.println(cell.getRichStringCellValue().getString());
        break;
      case Cell.CELL_TYPE_NUMERIC:
        if(DateUtil.isCellDateFormatted(cell)) {
          System.out.println(cell.getDateCellValue());
        } else {
          System.out.println(cell.getNumericCellValue());
        }
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        System.out.println(cell.getBooleanCellValue());
        break;
      case Cell.CELL_TYPE_FORMULA:
        System.out.println(cell.getCellFormula());
        break;
      default:
        System.out.println();
        }
    }
}

回答by Gagravarr

You may find this answer helpful: How to get columns from Excel files using Apache POI

您可能会发现此答案很有帮助:How to get columns from Excel files using Apache POI

That has all the code you need to extract all the numeric values from one column, whether they're a number cell or a formula cell containing a number result. I suspect you'll need something like that.

这具有从一列中提取所有数值所需的所有代码,无论它们是数字单元格还是包含数字结果的公式单元格。我怀疑你会需要这样的东西。