Bug 59670

Summary: Sheet.getRow().getCell() gets the value from the next sheet along
Product: POI Reporter: cbradford
Component: HSSFAssignee: POI Developers List <dev>
Status: NEW ---    
Severity: normal    
Priority: P2    
Version: 3.14-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: Workbook

Description cbradford 2016-06-07 14:38:16 UTC
When using the HSSF library when I print the current sheet name it prints the correct one. If I then get the cell value of a known cell in that sheet it will return the value of the cell in the next sheet along.

How I get the sheet:
private static Sheet getSheet(Workbook wb, String sheetName){
        Sheet s = null;
        
        int numberOfSheets = workbook.getNumberOfSheets();
        
        for (int i = 0; i < numberOfSheets; i++) {
            
            s = wb.getSheet(sheetName);
            
            if( s.getSheetName().equals(sheetName)){
                return s;
            }
       }
       return s;
    }

How I get the cell value:
private static double getCellValue(Cell cell) {
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                return cell.getNumericCellValue();
            case Cell.CELL_TYPE_BLANK:
                return 0;
        }
      return 0;
}

double DOLabour = getCellValue(s.getRow(16).getCell(4));
Comment 1 Andreas Beeker 2016-06-07 15:55:23 UTC
Please add also your example sheet
Comment 2 cbradford 2016-06-08 08:02:11 UTC
Created attachment 33925 [details]
Workbook
Comment 3 Nick Burch 2016-06-08 12:33:28 UTC
Won't the logic of your sheet selection loop mean that if the name doesn't match, the last sheet will always be returned + used? Could that actually be the cause of your issue?

If not, any chance you could turn your test code into a junit unit test that we can use with your attached file to investigate more?
Comment 4 cbradford 2016-06-08 12:42:13 UTC
I have stepped through the code and if I am looking for the "PP1" sheet the function will return the "PP1" sheet and it will print this out using s.getSheetName(). When I then get the cell value using:

How I get the cell value:
private static double getCellValue(Cell cell) {
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                return cell.getNumericCellValue();
            case Cell.CELL_TYPE_BLANK:
                return 0;
        }
      return 0;
}

double DOLabour = getCellValue(s.getRow(16).getCell(4))

It will actually return the value of the "Application" sheet not the "PP1" sheet.

I have also noticed that when I loop through the sheets the first sheet is called "Module1" not "PP1".
Comment 5 cbradford 2016-06-15 07:13:15 UTC
Have you had any further developments with this bug?
Comment 6 Andreas Beeker 2016-06-15 07:40:04 UTC
(In reply to cbradford from comment #5)
> Have you had any further developments with this bug?

I had a quick try on this and can only confirm the bug.
Althougn the bound name is correct, I suspect that the vba sheet is causing an error with the sheet references ... but maybe someone else with a deeper knowledge of hssf can jump in?