Home » excel » excel – Java POI remove cell

excel – Java POI remove cell

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

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