When you call HSSFSheet#setSelected(boolean) to change the selected sheet, and save the workbook and open with Excel Application,you will see it; 1."selected tab" is changed to the sheet as you call the method. 2.but the sheet "currently showing" is not the sheet tab selecting. Secondly,if you call HSSFSheet#setSelected(false) to all sheets,you can make the workbook with selecting no sheet. It isn't good interface,I feel.I feel the parameter of the method should be "void" and works like HSSFSheet#setSelected(true).
Created attachment 5196 [details] Testcase to find this problem.Please do it and open "Output.xls" with Excel Application.
Created attachment 6265 [details] PATCH to fix (with some Proposals)
I searched the way to fix it,and found; To change the selection correctly,we must cange both WindowOneRecord stored at Workbook and WindowTwoRecord stored at Sheet. So,we can't make the method for this function at HSSFSheet,but only at HSSFWorkbook. Plz test and eval the PATCH with the testcase a little changed from I submitted. <s2.setSelected(true); >b.setSelectedSheet("Sheet1"); or >b.setSelecteSheetAt((short)1); Regards.
I know there is an another way, Add an instance-field "the reference to parent Workbook object" at Sheet, and once the event to send other Sheets or Workbook happend at a Sheet(like Sheet#setSelected(boolean)),the method send the event by using the reference. I feel it is cool,but I don't know we can cange the reference-structure of objects or not...
Sorry,I didn't know that we can select multiple sheet-tabs with "ctrl+mouse click"etc. at Excel XP.So this proposal is invalid.Please forget the attatchments id=5196 & id=6265. But "There is no way to change the displayed sheet with usermodel APIs" is an another problem.Please add a way to do it. I found that we can do it with hssf.record.WindowOneRecord#setSelectedTab(short)
Created attachment 6416 [details] PATCH to do it.
Please apply the PATCH and execute it and check Test1.xls and Test2.xls with Excel Application. import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class Test { public static void main(String[] args) throws Exception{ HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet s1 = book.createSheet("Test1"); HSSFSheet s2 = book.createSheet("Test2"); book.write(new FileOutputStream("Test1.xls")); s1.setSelected(false); s2.setSelected(true); book.setSelectedSheet("Test2"); book.write(new FileOutputStream("Test2.xls")); } }
It appears that a variant of the patch 6416 was applied at some stage, but this bug never closed. The method HSSFWorkbook.setSelectedTab is now available & it does what the bug decribes. Closing.... Jason