Bug 43088 - Excel file can't be loaded if comments exceed a size of 4111 characters
Summary: Excel file can't be loaded if comments exceed a size of 4111 characters
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.0-FINAL
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-08-10 08:28 UTC by Jan Dostert
Modified: 2019-05-16 03:57 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Dostert 2007-08-10 08:28:22 UTC
Hi,

this bug seems to be similar to bug 42920 (Excel Comments).

When attaching comments to a cell with poi-3.0.1-FINAL-20070705.jar and the
comments exceed a size of 4111 characters, the file can't be loaded by Excel any
more (I tried Excel 2003 and Excel 2007).

There are no problems in saving the file. However, when opening the file with
Excel, I get the error message "Excel found unreadable content in foo.xls".

Attached a small test case which reproduces the problem. The created excel file
works fine if the comment size is up to 4111 characters. But with a comment size
of 4112 characters, the excel file can't be loaded any more.

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Comments {

    public static void main(String args[]) throws Exception {

        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet();
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell((short)0);

        // works  with 4111
        // broken with 4112
        int size = 4112;
        
        StringBuffer toolTip = new StringBuffer(size);
        for (int i = 0; i < size; i++) {
            toolTip.append(".");
        }
        
        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short)0, 0,
(short)1, 1);
        HSSFComment comment = patriarch.createComment(anchor);
        comment.setString(new HSSFRichTextString(toolTip.toString()));
        cell.setCellComment(comment);
        
        workBook.write(new FileOutputStream("foo.xls"));
    }
}

Regards,
Jan
Comment 1 Nick Burch 2007-08-15 04:57:51 UTC
Could you try the following:
* Add a comment using POI of ~4000 characters
* Take a copy of that file, and load it in Excel
* Edit the comment in Excel, so that it's >4112 characters long
* Compare the two files (hssf.dev and poifs.dev), and spot how excel has saved
the large comment

Once we know how Excel manages to generate larger comments, we can figure out
the best way forward for POI to do the same
Comment 2 Yegor Kozlov 2007-08-24 11:18:00 UTC
I confirmed the problem. I could also reproduce it with HSSFTextbox - if I set
text longer than 4111 characters the xls document gets corrupted. 

It's clear what's wrong. Excel records can't be longer than 8228 bytes, and 
text.length()*2 + header gets greater that the limit.

BTW, POI fails  to read such "bad" workbooks although there are no warnings
while saving it.

Here is the stack trace:
Caused by: org.apache.poi.hssf.record.RecordFormatException: The content of an
excel record cannot exceed 8224 bytes
	at
org.apache.poi.hssf.record.RecordInputStream.nextRecord(RecordInputStream.java:108)
	at org.apache.poi.hssf.record.TextObjectRecord.fillFields(TextObjectRecord.java:47)



Regards,
Yegor
Comment 3 Yegor Kozlov 2007-08-26 08:08:50 UTC
Fixed.
Record overflow wasn't properly handled in TextObjectRecord. 
Now it should work fine. Cell comments and Text Boxes can hold strings of any 
length.

Regards,
Yegor