簡體   English   中英

如何使用Java Apache POI從excel中刪除整行?

[英]How to delete an entire row from excel using Java Apache POI?

我想從Excel中刪除整行,

我試過了removeRow

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

shiftRows

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

但是它僅刪除行中的值,而不刪除整個行。

即使我也遇到了同樣的問題,但經過幾次研究后發現

有一個錯誤/或者他們可能更改了Apache Poi的<version>4.0.0</version> and <version>4.0.1</version>的行為,

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>

並使用更好的代碼來像這樣刪除行

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版本中對其進行更新

謝謝你能幫到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM