Excel单元格数据超过32767报错问题处理

Excel单元格数据超过32767报错问题处理

EasyExcel描述 EasyExcel是一款基于Java的简单、省内存的读写Excel的开源项目。官网。使用起来确实比较方便,但是对于一些比较复杂的场景,比如多单元格,现在的版本兼容不是很好,不过效率和使用上确实体验还可以。

问题描述:

不过今天在做一个数据量很大的Excel表导出的时候,遇到一个异常,这个字段数据量太多了,导出时候直接抛出如下异常:

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

看起来好像是超过框架限制了

在poi3.7.1版本找到代码

org.apache.poi.hssf.usermodel.HSSFCell#setCellValue(org.apache.poi.ss.usermodel.RichTextString):

public void setCellValue(RichTextString value)
    {
        int row=_record.getRow();
        short col=_record.getColumn();
        short styleIndex=_record.getXFIndex();
        if (value == null)
        {
            notifyFormulaChanging();
            setCellType(CellType.BLANK, false, row, col, styleIndex);
            return;
        }

        if(value.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){
            throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
        }

        if (_cellType == CellType.FORMULA) {
            // Set the 'pre-evaluated result' for the formula
            // note - formulas do not preserve text formatting.
            FormulaRecordAggregate fr = (FormulaRecordAggregate) _record;
            fr.setCachedStringResult(value.getString());
            // Update our local cache to the un-formatted version
            _stringValue = new HSSFRichTextString(value.getString());

            // All done
            return;
        }

        // If we get here, we're not dealing with a formula,
        //  so handle things as a normal rich text cell

        if (_cellType != CellType.STRING) {
            setCellType(CellType.STRING, false, row, col, styleIndex);
        }
        int index = 0;

        HSSFRichTextString hvalue = (HSSFRichTextString) value;
        UnicodeString str = hvalue.getUnicodeString();
        index = _book.getWorkbook().addSSTString(str);
        (( LabelSSTRecord ) _record).setSSTIndex(index);
        _stringValue = hvalue;
        _stringValue.setWorkbookReferences(_book.getWorkbook(), (( LabelSSTRecord ) _record));
        _stringValue.setUnicodeString(_book.getWorkbook().getSSTString(index));
    }
解决方法:

在网上搜索,整理一下两种处理方法:

复制org.apache.poi.ss.SpreadsheetVersion代码到项目里,包路径这些不能修改,然后找到EXCEL2007(0x100000,
0x4000, 255, Integer.MAX_VALUE, 64000,
32767);,修改最后面的值,可以修改为Integer.MAX_VALUE `

通过反射机制修改

public static void resetCellMaxTextLength() {
    SpreadsheetVersion excel2007 = SpreadsheetVersion.EXCEL2007;
    if (Integer.MAX_VALUE != excel2007.getMaxTextLength()) {
        Field field;
        try {
        	// SpreadsheetVersion.EXCEL2007的_maxTextLength变量 
            field = excel2007.getClass().getDeclaredField("_maxTextLength");
            // 关闭反射机制的安全检查,可以提高性能
            field.setAccessible(true);
            // 重新设置这个变量属性值
            field.set(excel2007,Integer.MAX_VALUE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

参考资料
https://www.jianshu.com/p/9e73399b6c38
https://blog.csdn.net/yandype/article/details/122718307

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值