Apache POI移位行 [英] Apache POI Shift Row

查看:173
本文介绍了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",您不会将所有行从插入点移到末尾.相反,您只需要移动数量"行-这很可能导致超出"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天全站免登陆