Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: The workbook already contains a sheet of this name

Tags:

java

excel

I am getting an exception when inserting more than 1.2 million records. Successfully inserted 1 047 000 records in 1 sheet and when creating the new sheet to insert the rest of the records, I am getting

java.lang.IllegalArgumentException: The workbook already contains a sheet of this name

I am using this condition for creating the new sheet

if(rowCount>1047000){
    wsSheet = createSheet(sheetName, xssfWorkbook);
    createHeader(wsSheet, columsnList, xssfWorkbook, null,listResult.isNameFieldRequired());
    rowCount = 1;
}

Tried of changing the sheetName in

wsSheet = createSheet(sheetName, xssfWorkbook) as sheetName+"1"

but still the same exception.

like image 567
Dileep Avatar asked Sep 03 '25 10:09

Dileep


2 Answers

There is some oddity in the way the org.apache.poi.ss.usermodel.Sheet checks if a string is already used as a sheet's name: the candidate name is truncated using name.substring(0, 31) and compared to existing names truncated the same way, so you have to make strings differ in at least a character in this range.

like image 141
dimirsen Z Avatar answered Sep 04 '25 23:09

dimirsen Z


XSSFSheet newSheetName = workbook.createSheet("sheetName"+1);

That should simply work. Then depending on your code, you may need to increase the newSheetName variable scope.

like image 29
Perry Avatar answered Sep 04 '25 22:09

Perry