I am trying to delete all rows in an excel using the below code:
InputStream oFile=new FileInputStream(DefectExcel);
Workbook oWB= WorkbookFactory.create(oFile);
Sheet sheet= oWB.getSheet(Sheet1);
Row oRow= sheet.getRow(i);
for (int i =0;i <=sheet.getLastRowNum(); i++) {
sheet.removeRow(sheet.getRow(i));
System.out.println("Row Deleted");
}
Also tried using the below code as well:
InputStream oFile=new FileInputStream(DefectExcel);
Workbook oWB= WorkbookFactory.create(oFile);
Sheet sheet= oWB.getSheet(Sheet1);
Row oRow= sheet.getRow(i);
Iterator<Row> rowIte = sheet.iterator();
while(rowIte.hasNext()){
System.out.println("Row deleted");
rowIte.next();
rowIte.remove();
In both the above code, its printing Row deleted number of times the available row but the row originally is still not deleted from the excel.
Can someone please help why the rows are still not deleted from the excel and how can I remove all the rows in the excel?