How to delete contents of an Excel sheet in Java?

Shumon Saha picture Shumon Saha · Aug 4, 2011 · Viewed 23.7k times · Source

How to delete contents of an Excel sheet in an Excel workbook, using Java SE and Apache POI?

Answer

Thirupathi Sriramoji picture Thirupathi Sriramoji · Mar 5, 2014

As mentioned in previous comments

Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
   sheet.removeRow(row);
}

this code throwing ConcurrentModificationException to me. So, I have modified the code and it's working fine. Here is the code:

Sheet sheet = wb.getSheetAt(0);
Iterator<Row> rowIte =  sheet.iterator();
while(rowIte.hasNext()){
    rowIte.next();              
    rowIte.remove();
}