从单元格值Apache POI获取单元格索引

SDS*_*SDS 6 java excel apache-poi

Environment    Status    Version    PatchNumber
Windows        Live      1.0        2
Unix           Live      2.0        4
Mac            Live      1.3        8
Run Code Online (Sandbox Code Playgroud)

如果我在excel中有上面显示的数据,我如何使用文本访问PatchNumber的cellNumber

XSSFRow row = (XSSFRow) rows.next();
我想访问row.getCellNumber("PatchNumber");//注意这个方法在Apache POI中不存在.

Gag*_*arr 7

我想我明白你在追求的是什么 - 你想知道哪一列中包含"Patch"这个词的第一行?如果是这样,您需要做的就是:

Sheet s = wb.getSheetAt(0);
Row r = s.getRow(0);

int patchColumn = -1;
for (int cn=0; cn<r.getLastCellNum(); cn++) {
   Cell c = r.getCell(cn);
   if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
       // Can't be this cell - it's empty
       continue;
   }
   if (c.getCellType() == Cell.CELL_TYPE_STRING) {
      String text = c.getStringCellValue();
      if ("Patch".equals(text)) {
         patchColumn = cn;
         break;
      }
   }
}
if (patchColumn == -1) {
   throw new Exception("None of the cells in the first row were Patch");
}
Run Code Online (Sandbox Code Playgroud)

只需循环遍历第一行(标题)行中的单元格,检查它们的值,并在找到文本时记下您所在的列!