2016-02-13 32 views
-1

我有一个excel文件,表格从第一行和第三列开始。在第一行和第三行中有字母表。使用java通过Excel表格搜索特定字符

我想要做的是在第一行找到一个字母,然后在第三列找到另一个,然后找到落在它们相交处的int值(我使用的是Apache POI)。

我用

cell.getRichStringCellValue().getString().trim().equals(cellContent) 

让我参考,但它保持返回默认值。

谢谢。

+0

我用cell.getRichStringCellValue()的getString()。修剪()。等于(cellContent),让我参考,但它保持返回默认值。 – user4326614

+0

好的,在你的问题中添加一个[MCVE](http://stackoverflow.com/help/mcve),以显示你所尝试的内容以及任何相关的上下文。 –

回答

0

存在类似字母的问题,所以代码会找到两个单元格,然后不返回任何内容。所以在excel文件中,我给第三列的单元格添加了一个字母,并在第一行添加了另一个字母以便区分它们。

for (Row row : sheet) 
    { 
     for (Cell cell : row) 
      { 
       if (cell.getCellType() == Cell.CELL_TYPE_STRING) 
        if (cell.getRichStringCellValue().getString().trim().equals(targetLetter)) 
         row1= row.getRowNum(); 
       if (cell.getCellType() == Cell.CELL_TYPE_STRING) 
        if (cell.getRichStringCellValue().getString().trim().equals(displayLetter)) 
         col1= cell.getColumnIndex(); 
       c= sheet.getRow(row1).getCell(col1); // c is of type Cell 
      } 
    }