2014-06-10 72 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);