我收到了这个错误:
工作簿已包含此名称的工作表
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++;
}
}
}
第if (workbookTemplate.getSheetIndex(sheetName)== -1)
行
应检查工作簿是否已包含此名称的工作表,但它不起作用。我仍然有错误。
感谢您的帮助!
答案 0 :(得分:1)
您是否尝试过移动String sheetName = ...
圈内的while
?
您完成它的方式,它总是尝试在每个循环中删除并重新创建相同的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++;
}
}
}