The maximum number of cell styles was exceeded. You can define up to 4000 styles

时间:2025-04-23 08:37:16
POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时,

就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面

报错如下:

Caused by: : The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook
at (:1144)
at (:88)
at (:612)
at (:112)
at (:190)
at (:856)
at .invoke0(Native Method)
at (:39)
at (:25)
at (:597)
at (:191)
at (:276)
at (:68)
at (:88)
... 33 more

-------------示例--------------

错误示例

for (int i = 0; i < 10000; i++) {
    Row row = (i); 
    Cell cell = ((short) 0); 
    CellStyle style = ();
    Font font = (); 
    (Font.BOLDWEIGHT_BOLD); 
    (font); 
    (style);
}

改正后正确代码

CellStyle style = ();
Font font = ();
(Font.BOLDWEIGHT_BOLD);
(font);
for (int i = 0; i < 10000; i++) { 
    Row row = (i); 
    Cell cell = ((short) 0); 
    (style);
}

 

以上方法原地址:/hoking_in/article/details/7919530


方法二(不推荐,影响性能):

1.4000最大样式错误

: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook错误

找到中修改createCellStyle函数内的最大样式数量即可。重新打即可。

 

    public HSSFCellStyle createCellStyle()
    {
    	if(() == MAX_STYLES) {
            throw new IllegalStateException("The maximum number of cell styles was exceeded. " +
                    "You can define up to 4000 styles in a .xls workbook");
        }
        ExtendedFormatRecord xfr = ();
        short index = (short) (getNumCellStyles() - 1);
        HSSFCellStyle style = new HSSFCellStyle(index, xfr, this);
        (index); 
        return style;
    }

 


IE兼容问题

如果IE系列的浏览器出现了兼容问题(具体就是显示不出报表来),我已知的可能出现的问题有,Excel模板里有数字已字符串形式写入,或者是写日期格式的单元格出现了写入错误。具体的解决如下。

数字形式写入代码

	public static Cell writeNumericValue(Sheet sheet, int row, int column,
			Double value) {
		Row poiRow = (row);
		if (poiRow == null) {
			poiRow = (row);
		}
		Cell poiCell = (column);

		if (poiCell != null) {
			(poiCell);
		}
		poiCell = (column);
		(Cell.CELL_TYPE_NUMERIC);
		(value);
		return poiCell;
	}


写入日期代码

	public static Cell writeDateValue(Workbook book, Sheet sheet, int row,
			int column, Date value) {
		Row poiRow = (row);
		CreationHelper createHelper = ();
		if (poiRow == null) {
			poiRow = (row);
		}
		Cell poiCell = (column);

		if (poiCell == null) {
			poiCell = (column);
		}

		CellStyle cellStyle = ();
		(().getFormat(
				"yyyy-mm-dd"));
		if (value != null) {
			(value);
		} else {
			(new Date());
		}
		(cellStyle);

		return poiCell;
	}


 以上方法原地址:/blog/1105529