I'd like to programmatically select a sheet with the following function: getWorkbook().setSelectedTab((short)2); The sheet with index 2 is displayed correctly but its "sheet tab" on the left bottom corner doesn't seem to be selected (actually no tab is selected...). A selected tab is usually displayed with a white background color. Michael
I'm guessing we're not setting all the records we need to then Can you upload three files: * the original file, with some other tab selected * the file with tab 2 selected by poi * the file with tab 2 selected by excel Ideally, could you also use biffviewer to spot the difference between them? Clearly one or two records aren't being updated that need to be, so we need to identify these records (by comparing the 3 files)
Created attachment 21618 [details] Original File (Sheet 1 selected)
Created attachment 21619 [details] POI Outputfile (Sheet 3 displayed but tab not selected) FileOutputStream out = new FileOutputStream("test_hmi.xls"); InputStream input = RunImport.class.getResourceAsStream( "simple.xls" ); POIFSFileSystem fs = new POIFSFileSystem( input ); HSSFWorkbook wb = new HSSFWorkbook(fs); // Select last sheet wb.setSelectedTab((short)(wb.getNumberOfSheets()-1)); wb.write(out); out.close(); // Note that sheet 3 is displayed in the output file but its tab is not correctly selected (sheet 1 remains selected)
Created attachment 21621 [details] Correct file (POI output should look like this)
Fixed in svn r656893 Looks like some confusion between sheet selection and focus (starting in WINDOW2 record). Added new method setActiveSheet(int) to HSSFWorkbook. Changed comment on selSelectedTab(int) to emphasise distinction from setActiveSheet Added new method setSelectedTabs(int[]) for multiple tab(sheet) selection. Added new methods / deprecated old methods with misleading names. Added junit test cases.
I confirm, I am facing the same problem. It's not fixed. In same scenario, after copying a old workbook from then adding a sheet to it and saving it. And setting the new sheet as Active is not working. I tried below both ways. workBook.setActiveSheet(workBook.getSheetIndex(workBook.getSheet(sheetName))); workBook.setSelectedTab(workBook.getSheetIndex(workBook.getSheet(sheetName))); But none is working. Thanks Shiva
Neither this works too :( workBook.setFirstVisibleTab(workBook.getSheetIndex(workBook.getSheet(sheetName)));
Created attachment 24337 [details] sample code re-testing bug fix This code shows adding a new sheet to an existing workbook, and setting it both 'active' and 'selected'. More extensive junits were added with the bug-fix in svn r657136 .
You haven't supplied enough details to make it clear what is going wrong, so it is difficult to guess what mistake you have made. Perhaps you are using POI 3.0 or earlier. The fix was available from version 3.1 onwards. Perhaps you haven't read the javadoc properly. Note - in the typical scenario both wb.setActiveSheet(ix) and wb.setSelectedTab(ix) need to be called. It's not clear whether you tried this. In any case you should have seen the proper effects of calling either method in isolation. Perhaps you've found some more complex use-case which re-creates the bug in version 3.1 or later. If you are certain you have working code that calls both these methods and creates a workbook with the wrong sheet active or selected, please upload that example (like attachment (id=24337)) to demonstrate your assertion.