allenzzzxd
allenzzzxd

Reputation: 331

Java POI remove cell

I try to remove the cell by using :

HSSFCell cell = sheet.getRow(0).getCell(i);
sheet.getRow(0).removeCell(cell);

I thought this should have clear the value, but the value of the cell has just been changed from "012345678" to "0". The cell type is always string.

Also, the cell has comment, it has not been removed neither. Then I add this statement between the two above :

cell.removeCellComment();

It didn't work. Is there anything I forget to do? Thanks in advance.

Upvotes: 4

Views: 13343

Answers (3)

Peter
Peter

Reputation: 389

Me too the same issue and my solution nothing new, is make copy of table and exclude cells of original table that you need then remove original table

        int originalTablePos = paragrah.getDocument().getPosOfTable(Original_Table);
        // copy table from original to new table
        paragrah.getDocument().removeBodyElement(originalTablePos);

I hope this helps others with the same problem

Greetings

Upvotes: 0

belgther
belgther

Reputation: 2534

You can try to set the cell value to null or empty string.

Upvotes: 0

limc
limc

Reputation: 40168

I don't see anything wrong with your code. After you have removed the cell, did you remember to save the changes? The following code works fine for me.

String xlsxPath = "C:\\path\\test.xlsx";

Workbook wb = WorkbookFactory.create(new FileInputStream(xlsxPath));

// get first row
Row row = wb.getSheetAt(0).getRow(0);

// remove second cell from first row
row.removeCell(row.getCell(1));

// save changes
FileOutputStream fileOut = new FileOutputStream(xlsxPath);
wb.write(fileOut);
fileOut.close();

Upvotes: 2

Related Questions