Bug 20045 - Fail to write long string with ENCODING_UTF_16
Summary: Fail to write long string with ENCODING_UTF_16
Status: RESOLVED DUPLICATE of bug 19974
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 1.5.1
Hardware: PC Windows XP
: P3 blocker (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2003-05-19 22:05 UTC by Vicki Geng
Modified: 2004-11-16 19:05 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Vicki Geng 2003-05-19 22:05:38 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();

    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");
		} catch (Exception e) {
	private void createCellWithString(HSSFRow row,
	    int cellId, String cellStr) {
	    HSSFCell cell = row.createCell((short) cellId);
	private String createLongString() {
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<longStringSize; i++) {
		return sb.toString();
Comment 1 Avik Sengupta 2003-07-01 15:01:32 UTC

*** This bug has been marked as a duplicate of 19974 ***