You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Leigh <cf...@yahoo.com> on 2011/02/16 06:16:09 UTC

getSheetIndex("Sheet1") strangeness

The documentation says HSSFWorkbook.getSheetIndex(sheetName) returns -1 when the given sheet name does not exist. But for some reason its returning "0" when the supplied name is "Sheet1" AND the workbook is empty. Strangely, it _only_ seems to happen when the name is "Sheet1". Change the name to "Sheet0" or "Abracadabra" and the method returns the expected -1.  What is my tired brain missing :) ?

jvm 1.6 / poi-3.7-20101029.jar

Example:
String sheetName = "Sheet1";
Workbook wb = new HSSFWorkbook();
System.out.println("getNumberOfSheets = "+ wb.getNumberOfSheets());
System.out.println("getSheetIndex("+ sheetName +") = "+ wb.getSheetIndex(sheetName));

Results:
getNumberOfSheets = 0
getSheetIndex(Sheet1) = 0   <=== should be -1 
========================

-Leigh


      

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: getSheetIndex("Sheet1") strangeness

Posted by Leigh <cf...@yahoo.com>.
> I'd be tempted to add an extra check into the HSSFWorkbook
> level, where it won't drop down to the records when no
> sheets exist, which'd avoid this confusion. Hopefully other
> POI developers can chime in on the sanity of that...

Ah, okay.  For now I could just add an extra check of getNumberOfSheets() > 0 to ensure I do not pick up the internal record.

Thanks for the response.



      

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: getSheetIndex("Sheet1") strangeness

Posted by Nick Burch <ni...@alfresco.com>.
On Tue, 15 Feb 2011, Leigh wrote:
> The documentation says HSSFWorkbook.getSheetIndex(sheetName) returns -1 
> when the given sheet name does not exist. But for some reason its 
> returning "0" when the supplied name is "Sheet1" AND the workbook is 
> empty. Strangely, it _only_ seems to happen when the name is "Sheet1".

When a new HSSFWorkbook is created, InternalWorkbook creates a bunch of 
records required to be a valid empty new file. One of those is a single 
BoundSheetRecord, which is what you're picking up.

I'd be tempted to add an extra check into the HSSFWorkbook level, where it 
won't drop down to the records when no sheets exist, which'd avoid this 
confusion. Hopefully other POI developers can chime in on the sanity of 
that...

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: getSheetIndex("Sheet1") strangeness

Posted by Leigh <cf...@yahoo.com>.
> AND the workbook is empty. 

Yes, it is a stupid example. But I was just curious about the why of it. I looked over the source briefly ... and could not figure out how it was coming up with 0 ;-) 


      

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org