After I execute the following code on the attached Excel file, openning the excel file is impossible : Excel 97 display an error message "unable to read file". Excel file is just a new document including a forms component such as a textbox. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("workbook.xls")); HSSFWorkbook wb = new HSSFWorkbook(fs, true); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(1); if (row == null) row = sheet.createRow(1); HSSFCell cell = row.getCell((short)1); if (cell == null) cell = row.createCell((short)1); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue("a test"); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); (I'm using POI 2.5 FINAL)
Created attachment 11133 [details] Excel basic file with a textbox
Use of 2.0-final version (see bug #27497) solve the problem
I got this to work by making the following corrections to AbstractEscherHolderRecord.java (in package org.apache.poi.hssf.records). Replace the serialize() and getRecordSize() methods with the code below [CODE] public int serialize(int offset, byte[] data) { LittleEndian.putShort(data, 0 + offset, getSid()); LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4)); if (escherRecords.size() == 0 && rawData != null) { System.arraycopy( rawData, 0, data, offset + 4, rawData.length); } else { int pos = offset + 4; for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); ) { EscherRecord r = (EscherRecord) iterator.next(); pos += r.serialize(pos, data, new NullEscherSerializationListener() ); } } return getRecordSize(); } /** * Size of record (including 4 byte header) */ public int getRecordSize() { if (escherRecords.size() == 0 && rawData != null) { return rawData.length + 4; } else { int size = 4; for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); ) { EscherRecord r = (EscherRecord) iterator.next(); size += r.getRecordSize(); } return size; } } [/CODE] The problem is in serialization of the Escher records (MSODRAWING, MSODRAWINGGROUP, and MSODRAWINGSELECTION). Even though your spreadsheet contains no images, forms controls are positioned on the page using the Escher layer, so the new Escher code is involved. The new Escher code will not parse an the Escher records until you actually make a drawing. Until you use the ddf package, it just holds the data of each Escher record as raw bytes. But if you attempt to write these raw bytes back, there is a logic problem where the MSODRAWINGGROUP (and other) record is not reconstructed correctly -- namely, the required four byte BIFF record header is omitted.
Created attachment 11955 [details] CVS diff -u against HEAD
Resolved with many thanks to Michael.