简体   繁体   English

Excel 工作表 POI 工作簿已包含工作表

[英]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.

相关问题 使用JAVA创建Excel文件:工作簿已包含此名称的工作表 - Using JAVA to create Excel file: The workbook already contains a sheet of this name 如何使用Apache POI for Java将Excel工作表复制到另一个工作簿中? - How to copy a Excel Sheet into another Workbook using Apache POI for Java? 使用Apache POI for Java在现有Excel工作簿中创建新工作表 - Creating New Sheet In Existing Excel Workbook Using Apache POI for Java 无法使用apache POI在工作簿中创建新的Excel工作表 - Cannot create new excel sheet in a workbook using apache POI 从 POI 中的工作簿中删除工作表 - Delete sheet from WorkBook in POI java.lang.IllegalArgumentException: 工作簿已包含此名称的工作表 - java.lang.IllegalArgumentException: The workbook already contains a sheet of this name 在 Apache POI 中使用 Workbook 和 Sheet 类? - Use of Workbook and Sheet classes in Apache POI? Apache POI:运行工作簿创建代码两次会出现错误“工作表已存在”,即使在 workbook.close() 之后也是如此 - Apache POI : Running the workbook creation code twice gives error “Sheet already exists”, even after workbook.close() 如何加载500 MB的Excel POI工作簿并从中删除工作表。 - How to load 500 MB excel POI workbook and remove a sheet from it.without memory issue? 通过使用Apache POI生成的数据透视表隐藏Excel工作簿中的第一张表 - Hiding first sheet in excel workbook with pivot tables generated using Apache POI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM