Bug 19974

Summary: Fail to write long string with ENCODING_UTF_16
Product: POI Reporter: George Zhou <george_x_zhou>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major CC: vgeng
Priority: P3    
Version: 3.0-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description George Zhou 2003-05-16 00:23:55 UTC
Set each cell encoding to ENCODING_UTF_16.
Write 6 cells in a row.  The 2nd, 3rd, 4th and 5th cells have long strings 
(9000 characters).  The 1st and last are very short.
Try to open the generated excel, got "Not Enough Memory" or "Corrupted file".
See attached program.
However, the file will be fine with out the last cell (Comment out line 40 of 
the attached file) or without the encoding.

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
 
public class PoiTest 
{
	private int longStringSize = 9000;
	
	public static void main(String[] args) 
	{
		PoiTest pt = new PoiTest();
		pt.writeExcel();
	}

    public PoiTest() {
    }
    
	public void writeExcel(){
		try {
	    HSSFWorkbook wb = new HSSFWorkbook();
	    HSSFSheet sheet = wb.createSheet("new sheet");
	
	    // Create a row and put some cells in it. Rows are 0 based.
	    HSSFRow row = sheet.createRow((short)0);
	    // Create a cell and put a value in it.
	    
	    String longString = createLongString();
	    
	    createCellWithString(row, 0, "1");
	    createCellWithString(row, 1, longString);
	    createCellWithString(row, 2, longString);
	    createCellWithString(row, 3, longString);
	    createCellWithString(row, 4, longString);
	    createCellWithString(row, 5, "end");
	    
	    // Write the output to a file
	    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
	    wb.write(fileOut);
	    fileOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void createCellWithString(HSSFRow row,
	    int cellId, String cellStr) {
	    HSSFCell cell = row.createCell((short) cellId);
	    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	    cell.setCellValue(cellStr);
	}
	
	private String createLongString() {
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<longStringSize; i++) {
			sb.append('a');
		}
		sb.append(longStringSize);
		return sb.toString();
	}
}
Comment 1 Andy Oliver 2003-05-16 00:42:56 UTC
upgrade.
Comment 2 George Zhou 2003-05-16 17:37:00 UTC
Just tried with the latest version 1.10.  Still got the same problem.
Comment 3 Avik Sengupta 2003-07-01 15:01:33 UTC
*** Bug 20045 has been marked as a duplicate of this bug. ***
Comment 4 Andy Oliver 2003-07-24 16:34:56 UTC
This needs to be fixed, but I think it makes more sense to do it in 3.0.  9000
characters is an awfully long strong.  I suspect I know why.  I bet that we're
not continuing such a thing correctly....  Its suspiciously close to the max SST
record size.
Comment 5 Jason Height 2004-10-13 03:40:44 UTC
Fixed in head. Problem with sst serializer dealing with multiple continue records.