java poi删除特定行,如何使用Java Apache POI从excel中删除整行?

I want to delete an entire row from excel,

I have tried removeRow:

XSSFRow rerow = sheet1.getRow(1);

sheet1.removeRow(rerow);

and shiftRows:

int rowIndex = 1;

int lastIntext = sheet1.getLastRowNum();

sheet1.shiftRows(rowIndex+1, lastIntext, -1);

But it is deleting only values in row not the entire row.

解决方案

Even i faced the same problems but figured after a couple of researches and found

There was a bug/or they might have changed the behavior in 4.0.0 and 4.0.1 of Apache Poi

for

sheet1.shiftRows(rowIndex+1, lastIntext, -1);

Please use

org.apache.poi

poi

3.17

org.apache.poi

poi-ooxml

3.17

And use your code much better way to remove you row like this

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) {

sheet.removeRow(removingRow);

}

}

}

And it worked for me.

May be they might update it in next api releases

Thank you if it would have helped you

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值