Bug 53992

Summary: java.lang.RuntimeException: WINDOW2 was not found (Empty sheet)
Product: POI Reporter: Triqui <pedro.t.garcia>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.8-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Triqui 2012-10-11 11:40:30 UTC
I get this exception:

java.lang.RuntimeException: WINDOW2 was not found
	at org.apache.poi.hssf.model.InternalSheet.<init>(InternalSheet.java:326)
	at org.apache.poi.hssf.model.InternalSheet.createSheet(InternalSheet.java:163)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:296)
	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:88)


I debbuged InternalSheet to see why this was happening. And found that the excel file I'm processing ends with an empty sheet which only contains one BOF and one EOF records.
These are the last line of the BiffViewer output:
Offset=0x00008DA3(36259) recno=1907 sid=0x0809 size=0x0010(16)
[BOF RECORD]
    .version  = 0x0600
    .type     = 0x1000 (#error unknown type#)
    .build    = 0x15EC
    .buildyear= 1997
    .history  = 0x0000C0C1
    .reqver   = 0x00000306
[/BOF RECORD]

Offset=0x00008DB7(36279) recno=1908 sid=0x000A size=0x0000(0)
[EOF]
[/EOF]


So in InternalSheet(RecordStream rs) constructor, the following code throws the exception.

        if (windowTwo == null) {
            throw new RuntimeException("WINDOW2 was not found");
        }

I would like to know if this WINDOW2 record is mandatory when the sheet is empty.
Excel and LibreOffice open the file without problems (they just don't display the empty sheet).

I have fixed the error replacing the mentioned lines with the following and it seems to be working perfectly:
        if (windowTwo == null) {
            if (rra != null) {
                throw new RuntimeException("WINDOW2 was not found");
            }
            log.log(POILogger.ERROR, "WINDOW2 record not found. Trying to add one with default values");
            windowTwo = createWindowTwo();
            records.add(findFirstRecordLocBySid(EOFRecord.sid), windowTwo);
        }



Please, let me know if this solution is right or there is any other workaround or I should contact the creators of the excel file to ask them to fix it on their software.

Thank you.
Comment 1 Yegor Kozlov 2012-10-11 11:47:23 UTC
do you know the origin of the problematic file? Can you attach it? 

What happens if you open and re-save it in Excel or LibreOffice ? Does POI read the re-saved file ?

POI is being used in thousands installations across the world and so far no one complained on this exception. All test files that we've seen have a  WINDOW2 record . 

Yegor
Comment 2 Triqui 2012-10-11 13:00:47 UTC
The file is created from the 'export' feature of an application of one of our providers.
I'm trying to find out how the files are generated but it's not easy. Also I'm trying to get an example of one of those files without any sensible data in it so I can attach it.

As with every problem I found using POI re-saving the file fixes everything.

This file has another problem and I've reported a bug about it and you have asked me to attach the file already (I'm doing my best to get one I can attach).
The question I have is, since both Excel and LibreOffice open this file correctly, shouldn't POI try to do the same thing?

I understand your statement about this being a very unique case. But just fyi, I receive excel files every day from more than 250 different sources (and growing) so the sample of files I have to handle gives me the great opportunity to hit the weirdest issues :)
(This should sound as congratulatory comment, since I've only found 5 bugs with POI so far, and that means POI is working great for me).

Thanks.
Comment 3 Yegor Kozlov 2012-10-11 15:36:25 UTC
I generally agree with you. If Excel and LibreOffice tolerate files with missing WINDOW2 then POI should do so, but I will apply this fix only when see a sample file. 

Yegor
Comment 4 Yegor Kozlov 2012-10-26 11:54:51 UTC
changing status to NEEDINFO until a test file is provided
Comment 5 Triqui 2013-01-22 11:52:47 UTC
I have attached a test file in the bug 53984 (attachment 29880 [details])

The problem with that file is that it throws the exception reported in that bug first and only once you get around it the exception reported here is thrown.

The fix I proposed in a comment above this has been working without issues for some time now, but if you need anything from me to fix, just let me know.
Comment 6 Nick Burch 2014-07-31 13:33:22 UTC
This has been fixed as part of bug #53984 - we now warn + skip over these invalid bof / eof sets