Java – how to read specific excel columns using Apache poi

I encountered an excel problem when using Apache poi I can read across lines, but sometimes I'm in a situation where I just want to read a specific column

Therefore, only any specific column can be read, only column "a" or column "C"

I am using the Java language

Solution

Heikkim is right. Here are some sample code adapted from some of my code:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
...
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
  row = sheet.getRow(rowIndex);
  if (row != null) {
    String cellValueMay = null;
    for (int colIndex = 0; colIndex < colCount; colIndex++) {
      if (colIndex == theColIndexYouWant) {
        cell = row.getCell(colIndex);
        if (cell != null) {
          // Found column and there is value in the cell.
          cellValueMaybeNull = cell.getStringCellValue();
          break;
        }
    }

    // Do something with the cellValueMaybeNull here ...
  }
}

For colcount, use something like row getPhysicalNumberOfCells()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>