[英]Excel sheet POI Workbook already contains sheet
I'm getting this error :我收到此错误:
the workbook already contains a sheet of this name
工作簿已经包含一张同名的工作表
private void cleanDataSheets(XSSFWorkbook workbookTemplate) {
int numberOfQueries = extraction.getQueries().size();
// DATA[maxIndex][minIndex] --> ex : DATA00, DATA01, ..., DATA10
int maxIndex = 0;
int minIndex = 0;
String sheetName = DATA_SHEET_NAME + maxIndex + minIndex;
while (maxIndex < numberOfQueries) {
Sheet sheet = workbookTemplate.getSheet(sheetName);
if (sheet == null) {
maxIndex++;
minIndex = 0;
} else {
workbookTemplate.removeSheetAt(workbookTemplate.getSheetIndex(sheet));
if (minIndex == 0) {
if (workbookTemplate.getSheetIndex(sheetName) == -1) {
workbookTemplate.createSheet(sheetName);
}
}
minIndex++;
}
}
}
The line if (workbookTemplate.getSheetIndex(sheetName)== -1)
is supposed to check if the workbook already contains a sheet of this name but it doesn't work.行
if (workbookTemplate.getSheetIndex(sheetName)== -1)
应该检查工作簿是否已经包含此名称的工作表,但它不起作用。 I still have the error.我仍然有错误。
Thank you for your help !感谢您的帮助 !
Have you tried moving the String sheetName = ...
inside your while
loop?您是否尝试在
while
循环中移动String sheetName = ...
?
The way you have done it, it always try to remove and recreate the same sheetName
in every loop.按照您的做法,它总是尝试在每个循环中删除并重新创建相同的
sheetName
。
private void cleanDataSheets(XSSFWorkbook workbookTemplate) {
int numberOfQueries = extraction.getQueries().size();
// DATA[maxIndex][minIndex] --> ex : DATA00, DATA01, ..., DATA10
int maxIndex = 0;
int minIndex = 0;
while (maxIndex < numberOfQueries) {
String sheetName = DATA_SHEET_NAME + maxIndex + minIndex;
Sheet sheet = workbookTemplate.getSheet(sheetName);
if (sheet == null) {
maxIndex++;
minIndex = 0;
} else {
workbookTemplate.removeSheetAt(workbookTemplate.getSheetIndex(sheet));
if (minIndex == 0) {
if (workbookTemplate.getSheetIndex(sheetName)== -1){
workbookTemplate.createSheet(sheetName);
}
}
minIndex++;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.