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

x33g5p2x  于2022-05-28 转载在 其他  
字(2.4k)|赞(0)|评价(0)|浏览(478)

EasyExcel单元格数据超过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();
        }
    }
}

参考资料

相关文章

微信公众号

最新文章

更多