You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2003/05/20 00:05:39 UTC

DO NOT REPLY [Bug 20045] New: - Fail to write long string with ENCODING_UTF_16

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20045>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20045

Fail to write long string with ENCODING_UTF_16

           Summary: Fail to write long string with ENCODING_UTF_16
           Product: POI
           Version: 1.5.1
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: Blocker
          Priority: Other
         Component: HSSF
        AssignedTo: poi-dev@jakarta.apache.org
        ReportedBy: vgeng@ariba.com


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();
	}