Bug 28328

Summary: sheet selection does not work
Product: POI Reporter: Christian Sage <csage>
Component: HSSFAssignee: POI Developers List <dev>
Status: CLOSED FIXED    
Severity: normal    
Priority: P3    
Version: 2.5-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: other   
Attachments: WindowOneRecord's selectedTab and displayedTab are made accessible via usermodel api
Updated the patch file after recent changes for RICHTEXT

Description Christian Sage 2004-04-10 09:16:11 UTC
Using various methods I cannot get a second sheet to retain its status as 
selected after saving it. Because I am not 100% sure that it's not me being 
stupid I am including a test case here (after all I am new to POI and not too 
experienced in Java). I use Java 1.4.2_04 and POI-2.5-final on Windows XP Pro, 
and I open the resulting workbook in MS Excel 2002 SP-2. The test case follows 
after "---< cut". Neither of the three selection methods included (one active 
and two others commented out) work for me.

---< cut
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class PoiTest19 {
	
	public static void main(String[] args) {
		HSSFWorkbook workbook;
		HSSFSheet worksheet;
		HSSFFooter footer;
		HSSFRow row;
		HSSFCell cell;
		
		workbook = new HSSFWorkbook();
		worksheet = workbook.createSheet("empty sheet");
		worksheet = workbook.createSheet("sheet with rows");
		
		footer = worksheet.getFooter();
		footer.setRight("Page " + HSSFFooter.page() + " of " + 
HSSFFooter.numPages());
		
		for (int i = 0; i < 134; i++) {
			row = worksheet.createRow(i);
			cell = row.createCell((short) 0);
			cell.setCellValue(i * 1000 + i * (i + 1));
		}
		
		worksheet.setSelected(true);
		// workbook.getSheetAt(1).setSelected(true);
		// workbook.getSheetAt(workbook.getSheetIndex("sheet with 
rows")).setSelected(true);
		
		try {
			FileOutputStream ausgabeDatei = new FileOutputStream
("poi_test.xls");
			workbook.write(ausgabeDatei);
			ausgabeDatei.close();
		}
		catch(FileNotFoundException fnfE) {
			System.out.println("Datei nicht vorhanden!");
			fnfE.printStackTrace();
		}
		catch(IOException ioE) {
			System.out.println("Ein Ein-/Ausgabe-Fehler ist 
aufgetreten!");
			ioE.printStackTrace();
		}
	}
	
}
Comment 1 Jan-Martin Roth 2004-11-04 15:11:33 UTC
I am having the same problem generating an excel file with 3 sheets via POI
(poi-2.5-final-20040302.jar and Excel97 SR-2).

When I store the file and open it with excel, the 3rd sheet is shown.
Now I want to make sure, the 1st sheet (overview) shows up when the file is
opened by someone.
I tried the setSelected on the 1st sheet, but this only results in the tab for
the 1st sheet having white background, but the 2nd is shown to the user.
I found, I'm not the only one experiencing this problem:
http://java2.5341.com/msg/78833.html

So some people seem to be waiting for a solution. Thanks! 
Comment 2 nicolas advincula 2005-08-01 07:22:36 UTC
I have the same problem.
I have an excel file as template containing 12 sheets.
The default selected sheet is the first sheet (by the time the excel template 
was saved).
The program will select the sheet of the month that was selected from the 
screen.

Below is the code snippet:

workBook.getSheetAt(0).setSelected(false);
//month is zero-based
workBook.getSheetAt(reportMonth).setSelected(true);

I hope that this would be fixed.
I am using poi-2.5.1-final-20040804.jar
I also tried using poi-bin-3.0-alpha1-20050704 but still, it was not yet fixed.


Thank you very much!
Comment 3 Amol Deshmukh 2005-08-01 16:38:41 UTC
Created attachment 15841 [details]
WindowOneRecord's selectedTab and displayedTab are made accessible via usermodel api

HSSFSheet#setSelected(boolean) cause the "tab selection" in excel to be set -
which simply means that the tab is seen as selected but the actual data could
be from a different tab (sheet).

BiffViewer indicated that "Sheet selection" is actually controlled by
WindowOneRecord.selectedTab.

This patch adds setter/getters for the WindowOneRecord in Workbook which is
then used by newly added usermodel method HSSFWorkbook#setSelectedTab(short) to
set the selected sheet.
(Similarly, although not directly related to this bug, setDisplayedTab was
added to HSSFWorkbook to allow setting the default displayed "first" tab in
excel)

It must be noted that to get the exact effect of selecting a sheet you would
(after this patch) need to use:



for (...) { // cycle over all sheets in workbook
  // causes previous selection to be deselected
  sheet.setSelected(false);
}

sheet = workbook.getSheetAt(mySheetIndex);

// show the tab as selected
sheet.setSelected(true); 

// show the data frm selected sheet
workbook.setSelectedTab((short) mySheetIndex);
Comment 4 Amol Deshmukh 2005-08-24 18:32:03 UTC
Created attachment 16182 [details]
Updated the patch file after recent changes for RICHTEXT

This patch has now been applied to CVS (previous patch file was not applied to
CVS)

Basically, same as previous patch file: changes in this patch file are only due
to change of code in HSSFWorkbook and Workbook due to recent RICHTEXT changes.
Comment 5 Jason Height 2005-08-25 00:13:07 UTC
Amol,

If the patch has been applied to CVS, can the bug be closed?

Jason
Comment 6 Hafedh Ghamgui 2006-08-22 16:09:04 UTC
Comment on attachment 16182 [details]
Updated the patch file after recent changes for RICHTEXT

send me this patch
Comment 7 Amol Deshmukh 2006-08-22 17:20:53 UTC
(In reply to comment #6)
> (From update of attachment 16182 [details] [edit])
> send me this patch 
> 

Verified fixed in POI trunk and 3.0-alpha2.