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

查看:53
本文介绍了如何使用 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

有一个错误/或者他们可能已经改变了 4.0.0 中的行为和 4.0.1 的 Apache Poi对于

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天全站免登陆