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

2024-01-11

我在使用 Apache POI 时遇到 Excel 问题。我可以跨行读取,但有时我只想读取特定列。

那么是否可以读取任何特定列,例如仅读取“A”列或仅读取“C”列。

我正在使用Java为此的语言。


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 ...
      // break; ???
    }
  }
}

For the colCount使用类似的东西row.getPhysicalNumberOfCells()

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Apache POI 如何读取特定的 Excel 列 的相关文章

随机推荐