Apache POI 换行

Ya *_*ang 0 java apache excel 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();
}
Run Code Online (Sandbox Code Playgroud)

但不幸的是,它删除了行中的一些行以适应这种转变。删除的行似乎是随机的,它们实际上并不直接位于移位行的下方或上方。如果我移动不止一行,它们似乎发生在移动的行之间。

预先感谢您的帮助。

小智 5

API 的第二个参数是“endRow”——通过传递“currentRow+amount”,您不会将所有行从插入点开始移动到末尾。相反,您只需移动“amount”行 - 这很可能会导致覆盖“currentRow+amount”之外的所有行(如果有的话)。

我会这样做:

sheet.shiftRows(currentRow, sheet.getLastRowNum()+1, amount, true,true);
Run Code Online (Sandbox Code Playgroud)