2014-02-22 57 views
1

我使用xlsx。我需要刪除一行。假設第5行。這意味着,第6行應該成爲5,7應該成爲6 ...什麼命令刪除一行(apache poi)?

我試圖

sheet.removeRow(sheet.getRow(6)); 
sheet.shiftRows(6, 6, -1); 

但上面的命令僅刪除值。並留下空白單元格。但我需要在excel中模擬delete選項。

回答

3

你可以試試下面的代碼:

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

這將轉向該行,然後將其刪除。