Java 如何使用apache poi删除一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21946958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:33:29  来源:igfitidea点击:

How to remove a row using apache poi

javaapache-poi

提问by CHEBURASHKA

I tried to remove the third row

我试图删除第三行

THIS IS THE EDIT I made based on the comments of rgettman, Leo, zibi. Thank you.

这是我根据 rgettman、Leo、zibi 的评论所做的编辑。谢谢你。

public class MainTest {

    public static void main(String[] args) throws IOException {

        FileInputStream file  =   new FileInputStream(new  File("test.xlsx") );
        XSSFWorkbook wb = new XSSFWorkbook(file);
        XSSFSheet sheet = wb.getSheetAt(0);
        sheet.removeRow(sheet.getRow(3));
//      sheet.shiftRows(3, 3, -1);

        File outWB = new File("testResult.xlsx");
        OutputStream out = new FileOutputStream(outWB);
        wb.write(out);
        out.flush();
        out.close();


        System.exit(0);
    }

}

But this deletes values in a row but does not remove the row

但这会删除一行中的值但不会删除该行

采纳答案by zibi

If you are working with XLS files (not XLSX) then you should use HSSFWorkbook. I just tested the solution below and everything works fine:

如果您正在使用 XLS 文件(不是 XLSX),那么您应该使用 HSSFWorkbook。我刚刚测试了下面的解决方案,一切正常:

    File file = new File(....);
    FileInputStream fis = new FileInputStream(file);
    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sheet = wb.getSheetAt(0);

    sheet.shiftRows(3, 3, -1);

    File outWB = new File(.....);
    OutputStream out = new FileOutputStream(outWB);
    wb.write(out);
    out.flush();
    out.close();

or even beter use the Workbook factory, which does type recognizing for you:

甚至更好地使用工作簿工厂,它可以为您识别类型:

    Workbook wb = WorkbookFactory.create(file);
    Sheet sheet = wb.getSheetAt(0);
    sheet.shiftRows(3, 3, -1);

belowe you can find a function which does the row removal, it could be used both in xls and xlsx (tested;)).

下面你可以找到一个删除行的函数,它可以在 xls 和 xlsx 中使用(已测试;))。

Workbook wb = WorkbookFactory.create(file);
Sheet sheet = wb.getSheetAt(0);
removeRow(sheet, 2);
File outWB = new File(...);
OutputStream out = new FileOutputStream(outWB);
wb.write(out);
out.flush();
out.close();


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

回答by AppLend

Try better special function removeRow

尝试更好的特殊功能removeRow

Also, XSSFWorkbook work with .xlsx extension.

此外,XSSFWorkbook 使用 .xlsx 扩展名。

回答by Sankumarsingh

See the defnition of Shift rows:

参见 Shift 行的定义:

Shifts rows between startRow and endRow n number of rows. If you use a negative number, it will shift rows up. Code ensures that rows don't wrap around. Calls shiftRows(startRow, endRow, n, false, false);Additionally shifts merged regions that are completely defined in these rows (ie. merged 2 cells on a row to be shifted).

Shifts rows between startRow and endRow n number of rows. If you use a negative number, it will shift rows up. Code ensures that rows don't wrap around. Calls shiftRows(startRow, endRow, n, false, false);Additionally shifts merged regions that are completely defined in these rows (ie. merged 2 cells on a row to be shifted).

Specified by: shiftRows(...) in Sheet
Parameters:
    startRow the row to start shifting
    endRow the row to end shifting
    n the number of rows to shift

So in order to remove the 3rd row you need to use startRow = 3(zero based indexing so basically it is fourth row), endRow = sheet.getLastRowNum()and n = -1to shift selected rows i.e. from 4th to the last row by 1 row upward.

因此,为了删除您需要使用的第 3 行startRow = 3(基于零的索引,因此基本上它是第四行),endRow = sheet.getLastRowNum()并将n = -1选定的行(即从第 4 行移至最后一行)向上移动 1 行。