删除最后一行apache poi

时间:2014-02-24 03:47:35

标签: java apache-poi

这是一种删除行的常用方法

public static void removeRow(Sheet sheet, int rowIndex) {
    int lastRowNum = sheet.getLastRowNum();
    if (rowIndex >= 0 && rowIndex < lastRowNum) {
        sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
    }
    if (rowIndex == lastRowNum) {
        Row removingRow = sheet.getRow(rowIndex);
        if (removingRow != null) {

        // to verify that I am dealing with the last row
            System.out.println(sheet.getRow(lastRowNum).getCell(0).toString() ; 
        // I created a unique string for the last row so that to make sure everything is ok

            sheet.removeRow(removingRow);  // does not always work
        }
    }
}

但由于某种原因,最后一行并不总是被删除。我用System.out.println

仔细检查了它

有人遇到过这个吗?我该如何处理?

1 个答案:

答案 0 :(得分:1)

尝试这样做:

int lastRowNum = sheet.getLastRowNum()+ 1;