2014-06-10 77 views
0

我需要一種方法在預先存在的行之間插入新的單元格和/或行,而不刪除任何行。Apache POI移位行

我試着使用:

public static void addRow(File f, int amount, int currentRow) 
     throws Exception { 
    FileInputStream file = new FileInputStream(f); 
    XSSFWorkbook workbook = new XSSFWorkbook(file); 
    XSSFSheet sheet = workbook.getSheetAt(0); 
    sheet.shiftRows(currentRow, currentRow + amount, amount, true, true); 
    file.close(); 
    FileOutputStream out = new FileOutputStream(f); 
    workbook.write(out); 
    out.close(); 
} 

但不幸的是它會刪除一些行的路線,以適應變化。被刪除的行似乎是隨機的,它們實際上並不直接位於移位行或以上的位置。如果我移動了多行,它們似乎發生在兩個移位行之間。

非常感謝您的幫助。

+0

http://stackoverflow.com/questions/5785724/how-to-insert-a-row-between-two-rows-in-an-existing -excel與 - HSSF-Apache的POI – user432

回答

3

API的第二個參數是「endRow」 - 通過傳遞「currentRow +數量」,您不會將所有行從插入點開始移動到結尾。相反,您只需移動「數量」行 - 這可能會導致覆蓋超出「currentRow +數量」(如果有)的所有行。

我會做這種方式:

sheet.shiftRows(currentRow, sheet.getLastRowNum()+1, amount, true,true);