I have a program which changes multiple excel files in given directory. I need to remove multiple columns from excel file. For example, I want to remove all columns after 5th.
private void deleteColumns(Sheet sheet, int columnIndex) {
for (Row row : sheet) {
int j = 0;
for (Cell cell : row) {
if (j >= columnIndex) {
row.removeCell(cell);
}
j++;
}
}
}
I get ConcurrentModificationException.
I saw a post on how to delete a cell. Apache POI xls column Remove
This idea is clear for me. I will need to create a row, clone necessary cells there, and remove the row.
What are other solutions?
I also tried to create a substitute row, to clone cells which I need, and then remove the original row. I took this idea from here.
I would like to see some better solutions, with less code or more effective then removing cells one by one.