2014-02-24 34 views
2

這是由於某種原因,最後一排並不總是被刪除移除一行卸下最後一排的Apache POI

public static void removeRow(Sheet sheet, int rowIndex) { 
    int lastRowNum = sheet.getLastRowNum(); 
    if (rowIndex >= 0 && rowIndex < lastRowNum) { 
     sheet.shiftRows(rowIndex + 1, lastRowNum, -1); 
    } 
    if (rowIndex == lastRowNum) { 
     Row removingRow = sheet.getRow(rowIndex); 
     if (removingRow != null) { 

     // to verify that I am dealing with the last row 
      System.out.println(sheet.getRow(lastRowNum).getCell(0).toString() ; 
     // I created a unique string for the last row so that to make sure everything is ok 

      sheet.removeRow(removingRow); // does not always work 
     } 
    } 
} 

不過的常用方法。我再次檢查它與System.out.println

有沒有人遇到過這個?我該如何處理它?

+0

我已經嘗試過不同的次數相同的代碼......它似乎工作正常,並始終給予預期的結果。如果rowIndex值是不允許通過任何if條件,否則全部刪除。 – Sankumarsingh

回答

1

嘗試這樣做:

INT lastRowNum = sheet.getLastRowNum()+ 1;