加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用Apache POI如何读取特定的excel列

发布时间:2020-12-14 05:58:51 所属栏目:Java 来源:网络整理
导读:我在使用Apache POI时遇到了一个excel问题.我可以跨行阅读,但有时我处于某种情况,我只想阅读一个特定的列. 因此,只能读取任何特定的列,只有“A”列或“C”列. 我正在使用Java语言. 解决方法 heikkim是对的,这里是一些从我的一些代码改编的示例代码: import
我在使用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) {
    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 ...
  }
}

对于colCount,使用像row.getPhysicalNumberOfCells()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读