当任何行为空白时,将有两种情况。
- 首先,该行位于其他行之间,但永远不会初始化或创建。在这种情况下, Sheet.getRow(i)将为null。
- 其次,行已创建,其单元格可能会或可能不会被使用,但现在其所有单元格都为空白。在这种情况下, Sheet.getRow(i)不会为null 。(您可以通过使用 Sheet.getRow(i).getLastCellNum()进行检查 ,它将始终向您显示与其他行相同的计数。)
通常,第二种情况会发生。也许就您而言,这应该是原因。为此,您需要添加其他条件以检查所有单元格是否为空白。
for(int i = 0; i < sheet.getLastRowNum(); i++){ if(sheet.getRow(i)==null){ sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1); i--; continue; } for(int j =0; j<sheet.getRow(i).getLastCellNum();j++){ if(sheet.getRow(i).getCell(j).toString().trim().equals("")){ isRowEmpty=true; }else { isRowEmpty=false; break; } } if(isRowEmpty==true){ sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1); i--; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)