Summary: | Method Name.getSheetIndex() fails to return valid sheet index | ||
---|---|---|---|
Product: | POI | Reporter: | Wayne Holder <wholder> |
Component: | XSSF | Assignee: | POI Developers List <dev> |
Status: | RESOLVED INVALID | ||
Severity: | normal | ||
Priority: | P2 | ||
Version: | 3.6-FINAL | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | Windows 2000 | ||
Attachments: | Test file for example code |
You're using the wrong method there. getSheetIndex will tell you which Sheet a named range is DEFINED FOR, or -1 if it applies to the whole of the workbook (see the javadocs for details). Your named ranges apply to the whole of the workbook, which is why you get -1 You probably want name.getSheetName(), which gives you the sheet it references, not the sheet it's defined on |
Created attachment 25136 [details] Test file for example code In this example: InputStream inp = new FileInputStream("Book.xlsx"); // or .xls Workbook wb = WorkbookFactory.create(inp); int names = wb.getNumberOfNames() ; if (names > 0) { for (int ii = 0; ii < names; ii++) { Name name = wb.getNameAt(ii); String sName = name.getSheetName(); //int nameIdx = name.getSheetIndex(); // Hmm... doesn't work int nameIdx = wb.getSheetIndex(sName); // Alternate method } } the commented out line always returns -1, even when the named cell, or range is on a specific sheet. The following line shows a workaround. Note: this problem also happens for .xls files. Wayne