Bug 32255 - Exception when a formula contains long strings
Summary: Exception when a formula contains long strings
Status: RESOLVED WORKSFORME
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 2.5-FINAL
Hardware: All Mac OS X 10.0
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-15 21:33 UTC by Jesse Wilson
Modified: 2006-08-27 04:04 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jesse Wilson 2004-11-15 21:33:21 UTC
I am using poi-2.5.1-final-20040804.

I create a spreadsheet which contains a Hyperlink formula. If the label that is 128 characters or less, 
everything works perfectly. If the label is greater than 128 characters, an 
ArrayIndexOutOfBoundsException is thrown when the workbook is written to a file.

This exception should either be not thrown at all, or thrown earler when the formula is being 
constructed. 

Here is the stack trace:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1645
        at org.apache.poi.util.StringUtil.putCompressedUnicode(StringUtil.java:196)
        at org.apache.poi.hssf.record.formula.StringPtg.writeBytes(StringPtg.java:97)
        at org.apache.poi.hssf.record.FormulaRecord.serializePtgs(FormulaRecord.java:407)
        at org.apache.poi.hssf.record.FormulaRecord.serialize(FormulaRecord.java:361)
        at 
org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate.serialize(FormulaRecordAggregate.java
:84)
        at 
org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate.serialize(ValueRecordsAggregate.java:
153)
        at org.apache.poi.hssf.model.Sheet.serialize(Sheet.java:815)
        at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:794)
        at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:732)
        at ExcelHyperlink.main(ExcelHyperlink.java:34)


Here is the test program. Run this with a single argument of length > 128 to cause the exception to be 
thrown.

import java.util.*;
import java.io.*;
import org.apache.poi.hssf.usermodel.*;

/**
 * This class uses POI to create an XLS file which cannot be read by
 * Microsoft Excel.
 *
 * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
 */
public class ExcelHyperlink {
    
    public static void main(String[] args) throws IOException {
        if(args.length != 1) {
            System.out.println("Usage: ExcelHyperlink label");
            return;
        }
        
        // create a workbook, sheet, row and cell
        HSSFWorkbook workbook = new HSSFWorkbook();
        String sheetName = "Page";
        HSSFSheet sheet = workbook.createSheet(sheetName);
        HSSFRow row = sheet.createRow((short)0);
        HSSFCell cell = row.createCell((short)0);
        
        // create a hyperlink in our cell, labelled by the argument
        String target = "\"#" + sheetName + "!B2\"";
        String label = "\"" + args[0] + "\"";
        String formula = "HYPERLINK(" + target + ", " + label + ")";
        cell.setCellFormula(formula);

        // write the file
        FileOutputStream xlsOut = new FileOutputStream(new File("hyperlink.xls"));
        workbook.write(xlsOut);
        xlsOut.close();
    }
}
Comment 1 Jason Height 2006-08-27 11:04:50 UTC
Latest SVN code works a treat

Jason