【问题标题】:Apache POI Shift RowApache POI 移位行
【发布时间】:2014-06-10 03:58:11
【问题描述】:

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

我尝试使用:

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

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

提前感谢您的帮助。

【问题讨论】:

标签: java excel apache apache-poi


【解决方案1】:

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

我会这样做:

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

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    相关资源
    最近更新 更多