Java 如何使用 Apache POI 读取特定行?

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

How can I read specific rows with Apache POI?

javaexcelapacheapache-poi

提问by Grumme

I'm using the Apache POI library, but I have some data that I don't want to be read - So I need the program to start reading the file from a specific row.

我正在使用 Apache POI 库,但我有一些我不想被读取的数据 - 所以我需要该程序从特定行开始读取文件。

I want all the data from the cells and rows which comes after row 10, until the document is empty. I have tried with the following code.

我想要第 10 行之后的单元格和行中的所有数据,直到文档为空。我已尝试使用以下代码。

Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(0);

    Iterator<Row> iterator = firstSheet.iterator();
    Row getSchool = firstSheet.getRow(10);

    Iterator<Cell> cellIterator = getSchool.cellIterator();

    while (iterator.hasNext())
    {
        while (cellIterator.hasNext())
        {
         ...
        }
    }

But it will only give me all the data from the cells in row 10.

但它只会给我第 10 行单元格中的所有数据。

I'll be looking forward to hear from you :-).

我很期待收到你的来信:-)。

采纳答案by Diyarbakir

You're only getting the data from row 11 here:

您仅从此处获取第 11 行的数据:

Row getSchool = firstSheet.getRow(10);

See the documentation for Sheet.getRow(int rownum)

请参阅Sheet.getRow(int rownum)的文档

Returns the logical row (not physical) 0-based. If you ask for a row that is not defined you get a null. This is to say row 4 represents the fifth row on a sheet.

返回从 0 开始的逻辑行(非物理行)。如果您要求未定义的行,您会得到一个空值。也就是说,第 4 行表示工作表上的第五行。

Check the examples in the documentation on how to Iterate over rows and cells.

查看文档中有关如何迭代行和单元格的示例。

You can use something like:

您可以使用以下内容:

Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);

for (Row row : firstSheet) {
  for (Cell cell : row) {
     // Do something here
  }
}

If you want to iterate over all cells in a row check how to Iterate over cells, with control of missing / blank cells.

如果您想遍历一行中的所有单元格,请检查如何遍历单元格,并控制缺失/空白单元格

The CellIteratorwill only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel.

CellIterator只会返回该文件,这在很大程度上是那些具有价值或花式中定义的细胞,但它依赖于Excel中。

You could specify a Row.MissingCellPolicyas:

您可以将Row.MissingCellPolicy指定为:

Row.getCell(int, MissingCellPolicy)

Here's an example:

下面是一个例子:

int lastColumn = Math.max(row.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);

for (int cn = 0; cn < lastColumn; cn++) {
  Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
  if (c == null) {
    // The spreadsheet is empty in this cell
  } else {
    // Do something useful with the cell's contents
  }
}

回答by Shubham Jain

Refer below:

参考以下:

        String fileName = "D:\TestScripts.xls"; // file
        POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));
        HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
    //  HSSFSheet sheet = workbook.getSheetAt(0); //Get first Excel Sheet
        HSSFSheet sheet = workbook.getSheet("SheetName"); //Get data as per sheet name
        for (Row row : sheet) { // For each Row.
              Cell cell = row.getCell(0); // Get the Cell at the Index / Column you want.
              if(cell.getStringCellValue().equalsIgnoreCase("test")) {
                  System.out.println(cell.getRow().getLastCellNum());
                  for(int i=0;i<=cell.getRow().getLastCellNum()-1;i++) {
                      System.out.println(cell.getRow().getCell(i));
                  }
              }
           }

Remove below condition if you are not looking for any particular column data

如果您不查找任何特定列数据,请删除以下条件

 if(cell.getStringCellValue().equalsIgnoreCase("test"))