2014-02-12 38 views
0
 col1   col2 col3 col4 
row1 
row2 
row3 
row4 
row5 Row Labels sum1 sum2 sum3 
row6 blah   100  78  88 
row7 blah   200  5  8 
row8 blah   300  400 500 

我正在使用这个poi来查找“行标签”的行索引。使用要做到这一点的代码IM是:APACHE POI,如何查找单元格的行索引?

for(int i=0;i<r;i++){ 
     Row temprow=ws.getRow(i); 
     for(int j=0;j<ws.getRow(0).getLastCellNum();j++){ 
      Cell cell=temprow.getCell(j); 
      try{ 
       if(cell.getStringCellValue().equals("Row Labels")){ 
        temp=j; 
        break; 
       } 
      }catch(Exception e){ 
       continue; 
      } 
     } 
     break; 
    } 

有史以来温度的值始终为零何时到来,应该是未来的4, 不能似乎怎么找到我的错误。 帮助! 谢谢

+0

如果你试图通过它在调试步骤会发生什么事,你看到像您期望的流量? – Gagravarr

+0

好吧,当我去调试其亮点 和temp的值为零 – Anish6595

+0

其实,刚刚意识到你的图表在第一列中有文本“行标签”,是吗?如果是这样,那就是Index 0的列,所以'temp = j = 0'是完全可以预料的! (从你的代码,如果你想要的行号,保存我不是j) – Gagravarr

回答

1

促进对答案的评论...您的问题是,您正在记录j,列计数器的值,而不是i,行计数器!

如果是我,我会重写代码略有:

int headingRow = -1; 
for(int rn=0;rn<r;rn++){ 
    Row temprow=ws.getRow(rn); 
    if (temprow == null) { 
     // This row is all blank, skip 
     continue 
    } 
    for(int cn=0;cn<temprow.getLastCellNum();cn++){ 
     Cell cell=temprow.getCell(cn); 
     if (cell == null) { 
      // No cell here 
     } else { 
      try{ 
       // This bit is very nasty... 
       if(cell.getStringCellValue().equals("Row Labels")){ 
        headingRow=cn; 
        break; 
       } 
      }catch(Exception e){ 
       continue; 
      } 
     } 
    } 
    break; 
} 

好吧,我可能把它改写多一点,但至少代码有更多的有用的变量名来跟踪什么发生,检查空行和单元格等!

0

您需要行索引,以便分配i值而不是j值。 “行标签”是在第零列,因此只能将其返回总是0

for(int i=0;i<r;i++){ 
     Row temprow=ws.getRow(i); 
     for(int j=0;j<ws.getRow(0).getLastCellNum();j++){ 
      Cell cell=temprow.getCell(j); 
      try{ 
       if(cell.getStringCellValue().equals("Row Labels")){ 
        temp=i; // Check here 
        break; 
       } 
      }catch(Exception e){ 
       continue; 
      } 
     } 
     break; 
    } 
相关问题