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
我使用tha apahe 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 :(得分: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;
}
好吧,我可能会重写一下,但至少该代码有更多有用的变量名来跟踪发生的事情,检查空行和单元格等等!
答案 1 :(得分:0)
您需要行索引,因此请指定i
值而不是j
值。 “行标签”位于第0列,因此只返回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;
}