After looking a bit more carefully, it seems that defining Names and using them in a cell (as =A_Name) causes the following failue in 1.8.0 (and it seems 1.6.X onwards) - this does not cause a problem with 1.5.1. I've attached a slightly modified worksheet.xls example, (Im not sure of the attach policy here). The process is to take a worksheet in excel. To reproduce: 1. Add a defined name. (In excel Menu Insert/Names/Define) I inserted Control_Header as a reference to a cell with the string 'Test' in it (something like Sheet1!$A$4). 2. Add reference to name. I add a cell with '=Control_Header' in it. 3. Run the ReadWriteWorkbook example, to get the following error (I'd modified mine to take the filename as a variable). Cheers Mark pidgy:~/download/tmp/poi/test> java ReadWriteWorkbook workbook2.xls java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:42) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:30) at java.lang.reflect.Constructor.newInstance(Constructor.java:263) at org.apache.poi.hssf.record.RecordFactory.createRecord(RecordFactory.java:252) at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:190) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:181) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:153) at ReadWriteWorkbook.main(ReadWriteWorkbook.java:26)
Created attachment 3323 [details] excel spreadsheet with =Names cell that reproduces the problem
Clean up your classpath. That stack trace generally means you've got two versions of POI trampling on one another.
In not sure who is wrong, possibly the excel document. In the document at : http://sc.openoffice.org/excelfileformat.pdf It defines "Name" type as: 3.10.2 tName (23H, 43H, 63H) This token contains the one-based index to a NAME record ($5.39). In BIFF2-BIFF4 this could be the index to an EXTERNNAME record ($5.23) too. From BIFF5 on an external name is represented by the token tNameX ($3.10.11). Default class: tNameR. Token tName, BIFF2: Offset Size Contents 0 2 One-based index to NAME record ($5.39) or EXTERNNAME record ($5.23) 2 5 Not used Offset in formula is 22 when it gets to NamePtg 22 67 23 1 24 0 25 0 26 0 (end of array) 67 == 43Hex bytes 23 - 24 would be the offset bytes 25-26 would be the (seemingly compulsary) 0 on the end. But NamePtg (constructor) expects three fields: field_1_ixti = LittleEndian.getShort(data, offset); field_2_label_index = LittleEndian.getShort(data, offset + 2); field_3_zero = LittleEndian.getShort(data, offset + 4); The fields being declared as: private short field_1_ixti; // unknown function private short field_2_label_index; private short field_3_zero; // reserved must be 0 Perhaps for Name type 0x43 differs from type 0x23. I've created a patch: public NamePtg(byte [] data, int offset) { offset++; - field_1_ixti = LittleEndian.getShort(data, offset); - field_2_label_index = LittleEndian.getShort(data, offset + 2); - field_3_zero = LittleEndian.getShort(data, offset + 4); + if (data.length >= offset + 6) { + field_1_ixti = LittleEndian.getShort(data, offset); + field_2_label_index = LittleEndian.getShort(data, offset + 2); + field_3_zero = LittleEndian.getShort(data, offset + 4); + } + else { + field_1_ixti = 0; + field_2_label_index = LittleEndian.getShort(data, offset ); + field_3_zero = LittleEndian.getShort(data, offset + 2); + } } And now it reads this field (and the rest of the spreadsheet with included graphics) - So this works for me, but will leave it to you to work out if this is the "correct" solution. (will also include as an attachement).
Hi Oliver, It's definately *not* a CLASSPATH problem - of that Im sure :-). Cheers Mark pidgy:~/download/tmp/jakarta-poi/test> echo $CLASSPATH /home/odonohue/download/tmp/jakarta-poi/build/jakarta-poi-examples-1.9.0-dev-20021003.jar:/home/odonohue/download/tmp/jakarta-poi/build/jakarta-poi-1.9.0-dev-20021003.jar:/home/odonohue/download/tmp/jakarta-poi/build/jakarta-poi-contrib-1.9.0-dev-20021003.jar:/home/odonohue/download/tmp/jakarta-poi/build/jakarta-poi-scratchpad-1.9.0-dev-20021003.jar:/home/odonohue/download/tmp/jakarta-poi/lib/core/commons-logging-1.0.jar:/home/odonohue/download/tmp/jakarta-poi/lib/endorsed/xalan-2.2.0.jar:/home/odonohue/download/tmp/jakarta-poi/lib/endorsed/xerces-2.0.0.jar:/home/odonohue/download/tmp/jakarta-poi/lib/endorsed/xml-apis.jar:/home/odonohue/download/tmp/jakarta-poi/lib/optional/log4j-core.jar:.
Created attachment 3329 [details] patch to handle "=Name" formula (it works, but Im no expert in excel so please check)
Fixed in latest CVS. Testcase added. Should work in 2.0pre3. Will certainly to into 2.0pre4