使用Apache POI如何读取特定的Excel列

sel*_*lva 18 java excel apache-poi

我在使用Apache POI时遇到excel问题.我可以阅读各行,但有时候我会想要只读一个特定的列.

因此,可以仅读取任何特定列,例如仅读取"A"列或仅读取"C"列.

我正在使用Java语言.

Chr*_*ssy 24

heikkim是对的,这里有一些示例代码改编自我的一些代码:

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) {
    Cell cell = row.getCell(colIndex);
    if (cell != null) {
      // Found column and there is value in the cell.
      cellValueMaybeNull = cell.getStringCellValue();
      // Do something with the cellValueMaybeNull here ...
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

对于colCount使用类似的东西row.getPhysicalNumberOfCells()


Jac*_*ack 5

  Sheet sheet  = workBook.getSheetAt(0); // Get Your Sheet.

  for (Row row : sheet) { // For each Row.
      Cell cell = row.getCell(0); // Get the Cell at the Index / Column you want.
  }
Run Code Online (Sandbox Code Playgroud)

我的解决方案,代码更简单.