通过其内容获取单元格
有没有办法获取单元格对象或坐标单元格中包含的数据?
例如,如果坐标为(1; 5)的单元格包含string"FINDME"
,我想要做一些像Workbook.GetCellByData(“FINDME”),它应该返回Cell对象或(1; 5)。
我在Apache POI网站上find了一个可能有用的代码片段 。 我可以只读整个工作簿,并用IF语句查找数据,但是这样很脏…
编辑 :我已经编码的“脏”的解决scheme如下:
public Cell getCellByContent(String data) { for (Row row : wb.getSheetAt(0)) { for (Cell cell : row) { if (cell.getCellType() == Cell.CELL_TYPE_STRING){ System.out.println(String.format("Found String type at (%s,%s) and read: %s", row.getRowNum(), cell.getColumnIndex(), cell.getStringCellValue())); if (cell.getStringCellValue() == data) { //HERE return cell; //HERE } //HERE } } } System.out.println("Can't find it bruh!"); return null;
由于某种原因,它在if语句中失败。 编号喜欢获取内容"%title%"
的单元格。
输出:
Found String type at (0,0) and read: %title% <------ IT'S RIGHT HERE! Found String type at (2,0) and read: Test Information ... Can't find it bruh!
有人有一个想法,为什么这是行不通的?
修复肮脏的解决scheme更换
if (cell.getStringCellValue() == data)
同
if (cell.getStringCellValue().equals(data))
我想我可以帮你。 您只需为行和列创build两个for()循环,然后键入Workbook.getCellValue(i,j)(i是行的编号,j是列的编号