Apache POI 换行 [英] Apache POI Shift Row

查看:52
本文介绍了Apache POI 换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种在预先存在的行之间插入新单元格和/或行而不删除任何行的方法.

I need a way to insert new cells and/or rows between pre-existing rows without deleting any of the rows.

我尝试使用:

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();
}

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

But unfortunately it deletes some rows down the line to accommodate the shift. The deleted rows seem to be random they aren't actually directly below the shifted rows or above. They seem to happen inbetween shifted rows if I shift more than one row.

预先感谢您的帮助.

推荐答案

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

The second argument of the API is "endRow" - by passing "currentRow+amount" you will not shift all rows starting from the insertion point up to the end. Rather, you will just shift "amount" rows - this could well cause an overwrite for all rows beyond "currentRow+amount" (if there are any).

我会这样做:

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

这篇关于Apache POI 换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆