Apache POI删除行并追加行 [英] Apache POI delete row and append row

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

问题描述

我正在使用apache-poi(3.16 and 4.0.0)生成Excel文件.我需要删除指定的行(删除行并移至最后一行),并将数据追加到最后一行之后.当我移动生产线时,发现它没有按预期工作.

I am using apache-poi(3.16 and 4.0.0) to generate an excel file. I need to delete the specified line (remove row and move to the last row) and append the data after the last line. When I moved the line, I found that it did not work as expected.

这些是我的代码(rowNum< lastRowNum):

These are my codes (rowNum < lastRowNum):

shiftRows1

sheet.shiftRows(rowNum, rowNum, sheet.getLastRowNum() - rowNum, true, false);

shiftRows2

sheet.shiftRows(rowNum + 1, sheet.getLastRowNum(), -1, true, false);

appendRow

setRowValue(sheet.getLastRowNum() + 1, objectArray);

我的问题

  1. shiftRows1 + appendRow + 4.0.0/3.16 + XSSF:清除rowNum并删除lastRowNum

  1. shiftRows1 + appendRow + 4.0.0/3.16 + XSSF : clear rowNum and delete lastRowNum

shiftRows1 + appendRow + 4.0.0/3.16 + HSSF:清除rowNum并清除lastRowNum

shiftRows1 + appendRow + 4.0.0/3.16 + HSSF : clear rowNum and clear lastRowNum

shiftRows2 + appendRow + 4.0.0 + XSSF:损坏的文件;修复后,删除lastRowNum并清除> = rowNum

shiftRows2 + appendRow + 4.0.0 + XSSF : damaged file; after repair, delete lastRowNum and clear rows >= rowNum

shiftRows2 + appendRow + 4.0.0/3.16 + HSSF:删除rowNum并且不能追加行(lastRowNum始终等于删除前的lastRowNum)

shiftRows2 + appendRow + 4.0.0/3.16 + HSSF : delete rowNum and can not append row(lastRowNum always equal to the lastRowNum before delete)

shiftRows2 + appendRow + 3.16 + XSSF:工作

shiftRows2 + appendRow + 3.16 + XSSF : working

测试后,不能使用shiftRows1.为什么HSSFappendRow之后不更新lastRowNum?我如何使用shiftRows23.16达到它?

After test, the shiftRows1 cannot be used. Why HSSF not update lastRowNum after appendRow? how can i achive it use shiftRows2 and 3.16?

推荐答案

我以以下方式处理此问题(apache-poi 3.16):

I dealt with this problem in the following way(apache-poi 3.16):

private void removeRow(int rowNum) {
    Row row = sheet.getRow(rowNum);
    if (row == null) return;
    sheet.removeRow(row);

    int lastRowNum = sheet.getLastRowNum();
    if (rowNum < lastRowNum) {
        // bad way: shift the removed row down
        sheet.shiftRows(rowNum + 1, lastRowNum, -1, true, false);
        // remove lastRow: avoid Sheet#getLastRowNum() return same value after Sheet#shiftRows
        if (workbook instanceof HSSFWorkbook) removeRow(lastRowNum);
    }
}

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

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