如何使用Java Apache POI从excel中删除整行? [英] How to delete an entire row from excel using Java Apache POI?

查看:1413
本文介绍了如何使用Java Apache POI从excel中删除整行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从excel中删除整行,

I want to delete an entire row from excel,

我尝试过removeRow:

XSSFRow rerow = sheet1.getRow(1);
sheet1.removeRow(rerow);

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

存在一个错误/或者它们可能已经更改了Apache Poi的<version>4.0.0</version> and <version>4.0.1</version>中的行为

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

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

请使用

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>

使用您的代码更好的方法来删除行,如下所示

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

它对我有用. 也许他们可能会在下一个api版本中对其进行更新

And it worked for me. May be they might update it in next api releases

谢谢您能为您提供帮助

这篇关于如何使用Java Apache POI从excel中删除整行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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