java.lang.IllegalArgumentException: The maximum length of cell contents (text) is 32,767 characters

在使用EasyExcel导出Excel时遇到异常,提示单元格内容最大长度为32767字符。问题分析涉及Apache POI库中的SXSSFCell#setCellValue方法。解决方案包括:自定义org.apache.poi.ss.SpreadsheetVersion类,或将EXCEL2007的最大文本长度通过反射设置为Integer.MAX_VALUE。这两种方法都能有效解决导出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

在使用EasyExcel导出Excel的过程中出现了如下异常:

IllegalArgumentException: The maximum length of cell contents (text) is 32,767 characters

分析:

在方法org.apache.poi.xssf.streaming.SXSSFCell#setCellValue(java.lang.String) 中:

    public void setCellValue(String value) {
        if (value != null) {
            this.ensureTypeOrFormulaType(CellType.STRING);
            //此处判断单元格中值的大小是否大于EXCEL2007.getMaxTextLength()
            if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
                throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
            }

            if (this._value.getType() == CellType.FORMULA) {
                if (this._value instanceof SXSSFCell.NumericFormulaValue) {
                    ((SXSSFCell.NumericFormulaValue)this._value).setPreEvaluatedValue(Double.parseDouble(value));
                } else {
                    ((SXSSFCell.StringFormulaValue)this._value).setPreEvaluatedValue(value);
                }
            } else {
                ((SXSSFCell.PlainStringValue)this._value).setValue(value);
            }
        } else {
            this.setCellType(CellType.BLANK);
        }

    }

我们在来看看EXCEL2007.getMaxTextLength()的大小是多少:

 问题解决:

方法一:

在自己的项目文件夹下创建org.apache.poi.ss.SpreadsheetVersion 类,复制poi中的该类源码,excel2007中的最后一个值改为int类型最大值。重试导出问题解决。

 方法二:

利用反射强制将EXCEL2007中的_maxTextLength属性值修改为Integer.MAX_VALUE

public static void resetCellMaxTextLength() {
        SpreadsheetVersion excel2007 = SpreadsheetVersion.EXCEL2007;
        if (Integer.MAX_VALUE != excel2007.getMaxTextLength()) {
            Field field;
            try {
                field = excel2007.getClass().getDeclaredField("_maxTextLength");
                field.setAccessible(true);
                field.set(excel2007,Integer.MAX_VALUE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

在EeayExcel调用之前执行:

 问题解决。

参考资料:

1、cell 内容最大长度 The maximum length of cell contents (text) is 32767 characters_yandype的博客-优快云博客The maximum length of cell contents (text) is 32767 charactershttps://blog.youkuaiyun.com/yandype/article/details/122718307

easyexcel: The maximum length of cell contents (text) is 32,767 characters - 腾逸 - 博客园easyexcel The maximum length of cell contents (text) is 32,767 characters 使用easyexcel向excel中写内容出现了单元https://www.cnblogs.com/zt007/p/14824395.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值