4

I build the code to meet my client requirement using org.apache.poi.ss.usermodel.Sheet class

Now I got a new requirement to create a new excel sheet in between two existing sheets. In the existing excel file there are already three sheets at the index numbers p,1,2. I want to create a sheet at index number 2 moving the sheet at index number 2 to 3.

I could able to find the sheets names in Excel file using the code:

for (int i = 0; i < wb.getNumberOfSheets(); i++) 
        {  
          System.out.println("Sheet name: " + wb.getSheetName(i));
        } 

Also, I could able to find the sheet index numbres in Excel file using the code:

System.out.println("Sheet name: " + wb.getSheetIndex("Retail - All"));

The code I used to create a new sheet is: Sheet failuresSheet= wb.createSheet("Failures"); This is creating a new sheet at end. Please let me know the correct code for my requirement.

Remember that I used the class org.apache.poi.ss.usermodel.Sheet to meet my requirement.

Please let me know how to create a sheet at Index no 2 moving the sheet at index no 2 to 3. Thanks you in advance.

1 Answer 1

11

Look at this javadoc, you could change the shhet order with that method. So you need:

wb.setSheetOrder("Failures",1); //the index is 0 based
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, my requirement is met. The code you have suggested is working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.