Bug 49188 - autoSizeColumn, ArrayOutOfBounds if more than Short.MAX_VAULE rows
autoSizeColumn, ArrayOutOfBounds if more than Short.MAX_VAULE rows
Status: RESOLVED INVALID
Product: POI
Classification: Unclassified
Component: HSSF
3.6
PC Windows XP
: P2 major with 8 votes (vote)
: ---
Assigned To: POI Developers List
:
: 50853 (view as bug list)
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2010-04-27 03:04 UTC by al
Modified: 2011-03-04 10:06 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description al 2010-04-27 03:04:01 UTC
Created a worksheet with more tham Short.MAX_VALUE rows and 3 columns.
Called HSSFSheet.autoSizeColumn on each of the three columns, which caused the exception below. Auto sizing works when number of rows is lower than Short.MAX_VALUE

java.lang.ArrayIndexOutOfBoundsException: -32735
	at java.util.ArrayList.get(Unknown Source)
	at org.apache.poi.hssf.model.WorkbookRecordList.get(WorkbookRecordList.java:50)
	at org.apache.poi.hssf.model.Workbook.getExFormatAt(Workbook.java:787)
	at org.apache.poi.hssf.usermodel.HSSFCell.getCellStyle(HSSFCell.java:906)
	at org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn(HSSFSheet.java:1727)
	at org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn(HSSFSheet.java:1662)
Comment 1 Yegor Kozlov 2010-05-22 15:34:01 UTC
Please post sample code to reproduce the problem.

The following code works fine to me:

    public static void main(String[] args) throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sh = wb.createSheet();
        for (int i = 0; i < Short.MAX_VALUE + 5; i++) {
            HSSFRow row = sh.createRow(i);
            for (int j = 0; j < 3; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellValue("cell[" + i + "," + j + "]");
            }
        }
        for (int j = 0; j < 5; j++) {
            sh.autoSizeColumn(j);
        }
    }

Yegor
Comment 2 Bhalchandra 2011-02-10 07:13:37 UTC
following code is failing with same exception : 

	public static void main(String[] args) throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sh = wb.createSheet();
        for (int i = 0; i < 65535; i++) {
            HSSFRow row = sh.createRow(i);
            for (int j = 0; j < 3; j++) {
            	CellStyle cellStyle = wb.createCellStyle();
            	CreationHelper createHelper = wb.getCreationHelper();
            	cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
                HSSFCell cell = row.createCell(j);
                cell.setCellStyle(cellStyle);
                Date date = new Date();
                cell.setCellValue("cell[" + i + "," + j + "]");
            }
        }
        for (int j = 0; j < 5; j++) {
            sh.autoSizeColumn(j);
        }
        FileOutputStream fileOut;
		try {
			fileOut = new FileOutputStream("workbook_MaxLimitTest4.xls");
			wb.write(fileOut);
		    fileOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (IOException ie) {
			ie.printStackTrace();
		}
		System.out.println("Done");
    }
Comment 3 Yegor Kozlov 2011-02-10 08:43:53 UTC
The maximum number of cell styles in a workbook is 4000, see http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx

Cell Styles must be shared. Do not create per-cell, otherwise you will get a “Too many styles” error in Excel when opening your workbook. Think of Cell Styles like CSS and create them like that.

http://poi.apache.org/faq.html#faq-N100EF

Yegor
Comment 4 Yegor Kozlov 2011-03-03 03:57:48 UTC
*** Bug 50853 has been marked as a duplicate of this bug. ***