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

时间:2022-06-01 20:44:07

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 :-).

我期待着你的消息:-)

1 个解决方案

#1


6  

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

你只得到第11行的数据:

Row getSchool = firstSheet.getRow(10);

See the documentation for Sheet.getRow(int rownum)

请参阅说明文档。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。如果你要求一个没有定义的行,你会得到一个null。这就是说,第4行表示表上的第5行。

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 CellIterator will 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.MissingCellPolicy as:

可以指定一行。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
  }
}

#1


6  

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

你只得到第11行的数据:

Row getSchool = firstSheet.getRow(10);

See the documentation for Sheet.getRow(int rownum)

请参阅说明文档。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。如果你要求一个没有定义的行,你会得到一个null。这就是说,第4行表示表上的第5行。

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 CellIterator will 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.MissingCellPolicy as:

可以指定一行。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
  }
}