Bug 47199

Summary: two Page Settings Blocks
Product: POI Reporter: Naveen <kumar.naveen>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: critical CC: kumar.naveen
Priority: P2    
Version: 3.5-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
URL: http://www.nabble.com/user/UserProfile.jtp?user=1396385
Attachments: utput file which is giving PSB problem
new output file without PSB problem
Giving PSB problem

Description Naveen 2009-05-15 05:31:27 UTC
Hi
I am getting the following error when trying to run the following test case in TestExcelExtractor.java: 

public void testImportMacroPSB() throws Exception {
		String filename = "C:\\poi-src-3.5-beta5-20090512\\poi-3.5-beta5\\testOLD\\test.xls";
		InputStream macro = new FileInputStream(filename);
		Workbook oldWb = new HSSFWorkbook(macro);
		assertNotNull(oldWb);
		  FileOutputStream out = new FileOutputStream("c:/testPSB.xls");
		  oldWb.write(out);
	        out.close();
			
	}


Exception in thread "main" java.lang.RuntimeException: two Page Settings Blocks found in the same sheet
	at org.apache.poi.hssf.model.Sheet.<init>(Sheet.java:241)
	at org.apache.poi.hssf.model.Sheet.createSheet(Sheet.java:160)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:288)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:202)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:318)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:299)
	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:60)
	at com.ericsson.ccrtool.excel.impl.write.ExcelSheetComparer.main(ExcelSheetComparer.java:29)

This is not similar to bug https://issues.apache.org/bugzilla/show_bug.cgi?id=46840. Here the exception is coming in line number 241 of Sheet.java

The test file I am using here is an old file (1 year old ), When I open the file and change some cell value then the test case is passed. But I dont want to open and save all the file each time when ever I am getting the above error. 


I have also run the the POI code BiffViewer.java for both the old file (Which is giving problem) and the new file (open the old file save the file make new file) and I found that there is difference in the TABID element of the output file. In the correct file the element_0 start with counter "1" which was "O" in the old file.

old file(PSB problem file)

[TABID]
    .elements        = 17
    .element_0 = 0
    .element_1 = 1
    .element_2 = 2
    .element_3 = 3
    .element_4 = 4
    .element_5 = 5
    .element_6 = 6
    .element_7 = 7
    .element_8 = 8
    .element_9 = 9
    .element_10 = 10
    .element_11 = 11
    .element_12 = 12
    .element_13 = 13
    .element_14 = 14
    .element_15 = 15
    .element_16 = 16
[/TABID]


New file (open and save the old file)
[TABID]
    .elements        = 17
    .element_0 = 1
    .element_1 = 2
    .element_2 = 3
    .element_3 = 4
    .element_4 = 5
    .element_5 = 6
    .element_6 = 7
    .element_7 = 8
    .element_8 = 9
    .element_9 = 10
    .element_10 = 11
    .element_11 = 12
    .element_12 = 13
    .element_13 = 14
    .element_14 = 15
    .element_15 = 16
    .element_16 = 17
[/TABID]

I am sorry to say that I cant share my test file as per company privacy rule. 

Please let me know if you could help me on this issue. 

This problem is coming with both Excel 2003 file and Excel 2007 file and I have taken the latest (14 May 2009) zip file from the POI 3.5 

Waiting for your reply.. 

Thanks and Regards 
Naveen Kumar
Comment 1 Josh Micich 2009-05-15 11:11:48 UTC
Another bug besides bug 46840 was fixed recently: bug 46953.  That fix was made on May 11, and if you were running that code, it would be impossible to get that exception from line 241.  In the most recent version of Sheet.java (since May 11) this exception would have been thrown from line 236: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/model/Sheet.java?annotate=773438

The top pair of frames in your stacktrace is matched by revisions 757520 to 773411 (Mar 23 bug 46840 - May 11 bug 46953)
I am closing this bug on the assumption that that you're running a slightly older version of POI than May 14.  If you experience a similar problem please re-open with the new details.

Nightly builds can be found here http://encore.torchbox.com/poi-svn-build/
The fix for bug 46953 found from this file: poi-source-3.5-beta6-20090511.zip onwards.


Thanks for doing the extra investigation using BiffViewer.  I am guessing that the changes Excel made to the TABIDs  is unrelated to your problem.

Hopefully you don't need to troubleshoot further, but if Excel 'fixed' the file for your problem, it's likely that it shuffled a few records around, and that information would be interesting.  If you pipe the BiffViewer output through "grep Offset=0x", that will extract only the record headers.  The resulting file will not have any proprietary data, but the ordering of biff records will be preserved and that would be enough to diagnose this bug.

*** This bug has been marked as a duplicate of bug 46953 ***
Comment 2 Naveen 2009-05-18 00:28:49 UTC
Hi, 

Sorry, the error is coming in line number 236 now. I have taken the latest jar files: 
poi-contrib-3.5-beta6-20090518.jar
poi-ooxml-3.5-beta6-20090518.jar
poi-scratchpad-3.5-beta6-20090518.jar
poi-3.5-beta6-20090518.jar

Following exception is throwing in the console. 
 
Exception in thread "main" java.lang.RuntimeException: two Page Settings Blocks found in the same sheet
	at org.apache.poi.hssf.model.Sheet.<init>(Sheet.java:236)
	at org.apache.poi.hssf.model.Sheet.createSheet(Sheet.java:162)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:288)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:202)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:318)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:299)
	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:60). 

This is coming with my old file also. The step to recreate is same as above.
Comment 3 Josh Micich 2009-05-18 10:09:02 UTC
Thanks for re-verifying the bug with the most recent POI build.

In general it is difficult to fix any problem without sample the code/data to reproduce it.  Can you please upload the BiffViewer summary as mentioned above (pipe the BiffViewer output through "grep Offset=0x").  It will help to have BIFF summaries for both files (the '1 year old' file and the one re-saved by Excel).
Comment 4 Naveen 2009-05-19 05:57:38 UTC
Created attachment 23687 [details]
utput file which is giving PSB problem
Comment 5 Naveen 2009-05-19 06:11:07 UTC
Created attachment 23688 [details]
new output file without PSB problem
Comment 6 Naveen 2009-05-19 06:17:45 UTC
PFA the information needed .
Comment 7 Josh Micich 2009-05-21 12:51:13 UTC
The files you uploaded are incomplete.  It appears that the output of BiffView has been truncated.  None of the sheet sub-streams are present, and even the workbook stream is incomplete.  I can see 49 BOUNDSHEET records which implies 49 sheet sub-streams are missing.

I am pretty sure that BiffView will not silently quit in the middle of a file. So either it crashed and you didn't mention that, or you have deliberately truncated the output hoping that we only needed to consider the record order in the first 5% of the workbook.  Perhaps you are concerned that even these BIFF header summaries constitute information that you must keep private.  I can't convince you otherwise, but at least let us know that's what you are thinking.


If you are not able to upload a complete BIFF summary, you could try single stepping the code in a debugger:
 - First (since there are many sheets), find out which sheet the error occurs on.  When the bug occurs, take at "this._sheets.size" 2 stack frames up (HSSFWorkbook.java:288). 
 - Next re-run your app and progress over all the previous sheets and stop at HSSFWorkbook.java:288 before reading the sheet with the problem.
 - Put a breakpoint on line Sheet.java:219, let the app execute to there.
 - Observe carefully how many and which records are read from the RecordStream in the PageSettingsBlock constructor.
 - Let the app run to the Sheet.java:219 breakpoint again.  Pay attention to which records (non-PSB) have been read from the RecordStream since creating the PageSettingsBlock, and the current record (which seems to belong to the PSB)
 - Make sure that execution _immediately_ proceeds to the exception at Sheet.java:236 (otherwise you haven't observed the exact conditions for the bug yet)

When you find out specifically what POI is doing wrong (and what Excel is doing 'right'), please reply here so we can incorporate this knowledge back into POI.
Comment 8 Naveen 2009-05-22 03:41:06 UTC
Hi, 

How do I get the sheet name? I have searched in the code but not able to get the sheet.

In Sheet.java : 
I have tried to print the "recSid" the value is coming as 41 (Dec) and 0x0029 (hex).

the value for " _psBlock != records.get(prevPsbIx) " is coming as true in my case, Is it correct or we have to use equals method for comparing the objects.
Please advise.
Comment 9 Josh Micich 2009-05-22 11:07:16 UTC
It seems like you are going with the debugging approach.

(In reply to comment #8)
> How do I get the sheet name? I have searched in the code but not able to get
> the sheet.

At stack frame (HSSFWorkbook.java:288), you can see the number of already loaded sheets with "_sheets.size()".  This also happens to be the index to the sheet currently being constructed, so that sheet name can be observed with:
workbook.getSheetName(_sheets.size());


> In Sheet.java :
> I have tried to print the "recSid" the value is coming as 41 (Dec) and 0x0029
> (hex).

That sid corresponds to BottomMarginRecord.  We are going to need more information than this (see Comment 7).  Basically we need the list of records (found within RecordStream rs) starting from the records that the _psBlock has, all the way through to the current record (seems to be BottomMarginRecord) and a little further, just to make sure there are no more PSB records after that.  I guess that execution was at line 234 when you made this observation.  That's an important detail to clarify too.


> the value for " _psBlock != records.get(prevPsbIx) " is coming as true in my
> case, Is it correct or we have to use equals method for comparing the objects.

Did you try out your suggestion "!_psBlock.equals(records.get(prevPsbIx))"?
Comment 10 Naveen 2009-05-25 02:33:00 UTC
Created attachment 23710 [details]
Giving PSB problem 


I have attached the file which is giving PSB problem. 

Please use this file directly, if you open and change any of the value then this wont throw exception.



Test Case :
================

	
	public void testImportMacroPSB() throws Exception {
		String filename = "C:\\poi-src-3.5-beta5-20090219\\poi-3.5-beta5\\testOLD\\excel.xls";
		InputStream macro = new FileInputStream(filename);
		Workbook oldWb = new HSSFWorkbook(macro);
		assertNotNull(oldWb);
		  FileOutputStream out = new FileOutputStream("c:/testPSB.xls");
		  oldWb.write(out);
	        out.close();
	}


Please let me you if anything else is required. 

Thanks 
Naveen Kumar
Comment 11 Naveen 2009-05-28 00:28:56 UTC
Hi

Please let  me know if any one is working on this.. 

Waiting for your reply . 

Thanks 
Naveen
Comment 12 Chris Lott 2009-05-28 11:48:57 UTC
I hit this problem today on a XLS (Excel 2003) file when reading it with HSSF from POI 3.5 beta 5 of Feb 19, 2009.  I too am working with proprietary data that I cannot upload, and I bet that any modification will cause the problem to vanish.  The one bright spot: I tried reading the file with the streaming HSSF approach (i.e., eventusermodel, HSSFListener, and friends), and that worked very well.
Comment 13 Naveen 2009-05-31 22:15:44 UTC
Hi Chris,

Thanks for your repley . 

I dont know how to use the event model and all. 

Please suggest me how to resolve the error, as my release is pending due to this error. 

Thanks
Naveen
Comment 14 Josh Micich 2009-06-01 11:53:53 UTC
Fixed in svn r780774

junits added

Thanks for taking the time to upload something that would reproduce the error.  The problem in the sample file (attachment id=23710) was that the margin records are not found with the rest of the PageSettingsBlock.  If Excel re-saves the file, it corrects that problem.  POI now does the same.

I'm not sure if if is of concern to you, but my Excel (2007) reports the data loss in the sample file ("Shared workbook revisions and custom workbook views may have been lost"). Same warning message from Excel after re-writing with POI.
Comment 15 Naveen 2009-06-02 04:39:21 UTC
Thanks a lot for the solution. 

Cheers!!!
Naveen