Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a row using apache poi

I tried to remove the third row

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

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

like image 476
CHEBURASHKA Avatar asked Dec 08 '22 09:12

CHEBURASHKA


2 Answers

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

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

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);
        }
    }
}
like image 80
zibi Avatar answered Dec 16 '22 11:12

zibi


See the defnition of Shift rows:

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 = -1 to shift selected rows i.e. from 4th to the last row by 1 row upward.

like image 28
Sankumarsingh Avatar answered Dec 16 '22 10:12

Sankumarsingh