How to delete, not clear row in Sheet ? Apache POI

后端 未结 1 1186
谎友^
谎友^ 2021-01-13 07:19

I\'ve faced with a problem. I don\'t know how to delete row without leaving afterwords empty row.

I am using Apache-POI 3.9 and I am getting error using next code :<

相关标签:
1条回答
  • 2021-01-13 07:38

    Try this code, it should work:

    for(int i = 0; i < sheet.getLastRowNum(); i++)
    {
        row = sheet.getRow(i);
        if(patternt.equals(getCurrentString(row)))
        {
            data.add(getDataFromRow(row));
            // sheet.removeRow(row);    NO NEED FOR THIS LINE
            sheet.shiftRows(row.getRowNum() + 1, sheet.getLastRowNum() + 1, -1);
            i--;
        }
    }
    

    You need to decrease i by one every time you delete one row. And get the last row number again by using getLastRowNum().

    0 讨论(0)
提交回复
热议问题