Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:41:17+00:00 2026-06-15T19:41:17+00:00

I have a WorkBook and I’m trying to delete sheets which names don’t match

  • 0

I have a WorkBook and I’m trying to delete sheets which names don’t match with a specific String.

Here is my code

XSSFWorkbook book = new XSSFWorkbook(new FileInputStream(excelName));                   
            for(int i=0;i<book.getNumberOfSheets();i++){
                System.out.println(book.getSheetAt(i).getSheetName());
                if(!book.getSheetAt(i).getSheetName().equals(sheetName)){
                    book.removeSheetAt(i);
                }
            }   

The code runs well, but it doesn’t perform the desired task

EDIT

The solution as said is to reverse the loop order. Here is a cleaner code

private void removeOtherSheets(String sheetName, XSSFWorkbook book) {       
            for(int i=book.getNumberOfSheets()-1;i>=0;i--){
                XSSFSheet tmpSheet =book.getSheetAt(i);
                if(!tmpSheet.getSheetName().equals(sheetName)){
                    book.removeSheetAt(i);
                }
            }       
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:41:18+00:00Added an answer on June 15, 2026 at 7:41 pm

    You should delete in reverse order, not forward order. Otherwise, you remove a sheet from the list and change the state of it for future loops.

    Thing about the case of sheets A, B, C and D. You iterate to i=1, and get B. You delete B. If you don’t re-fetch the row at 1, and move on to i=2, you’ll be on D and will have skipped C! Iterating through in reverse avoids this problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel Workbook which has some VSTO-based c# code. Given a known
I have a workbook with sheets having names: Student (1), Student (2), Student (3)
I'm relatively inexperienced in VBA code, but have a workbook that I am trying
I have an Excel workbook, which using VBA code that opens another workbook, copies
We have an Excel workbook which has c# VSTO code in it and two
I have an excel workbook which needs to query data from an HTTP server.
I have a workbook with two sheets. I would like to format the cell
I have a workbook,which contains ActiveX control button and form control button and macros
I have an Excel workbook which I want to scroll through the contents of
I have an Excel workbook with dependencies on code in other other Excel workbooks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.