使用 com.alibaba:easyexcel 导出excel文件时遇到的问题

1.字符长度超出限制

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

解决方案:

1. 把字符串截取前32767个字符
/**
 * sqlText 字段导出 excel 时的转化器
 */
public class SqlTextConverter implements Converter<String> {
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) throws Exception {
        // 截取 SQL 语句前 32767 个字符
        WriteCellData<String> cellData = new WriteCellData<>();
        String sqlText = context.getValue();
        String splitSqlText = sqlText.substring(0,32767);
        cellData.setData(splitSqlText);
        cellData.setType(CellDataTypeEnum.STRING);
        cellData.setStringValue(splitSqlText);
        return cellData;
    }
}

在实体类上加上注解

    @ExcelProperty(value = "SQL 语句", converter = SqlTextConverter.class)
    @ColumnWidth(100)
    private String sqlText;
2.项目启动时统一修改最大长度
import org.apache.poi.ss.SpreadsheetVersion;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * 统一修改 excel 列最大长度
 */
@Component
public class ApplicationCommandLine implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        SpreadsheetVersion spreadsheetVersion = SpreadsheetVersion.EXCEL2007;
        Field maxTextLength = spreadsheetVersion.getClass().getDeclaredField("_maxTextLength");
        maxTextLength.setAccessible(true);
        maxTextLength.set(spreadsheetVersion, Integer.MAX_VALUE);
    }
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 09:00:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 09:00:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 09:00:03       58 阅读
  4. Python语言-面向对象

    2024-07-19 09:00:03       69 阅读

热门阅读

  1. 浏览器的卡顿与react的解决思路

    2024-07-19 09:00:03       19 阅读
  2. Zabbix的安装部署及使用流程

    2024-07-19 09:00:03       25 阅读
  3. 速盾:cdn是啥?cdn是什么?

    2024-07-19 09:00:03       24 阅读
  4. Spark on YARN

    2024-07-19 09:00:03       23 阅读
  5. 算法日记day 13(删除字符串中的所有重复元素)

    2024-07-19 09:00:03       22 阅读
  6. 【html】html的基础知识(面试重点)

    2024-07-19 09:00:03       21 阅读
  7. Zookeeper集群搭建问题

    2024-07-19 09:00:03       23 阅读
  8. C++版OpenCV_02_几何变换

    2024-07-19 09:00:03       21 阅读
  9. Cartographers Lua配置参考文档

    2024-07-19 09:00:03       22 阅读
  10. Vue随笔【render函数的使用】

    2024-07-19 09:00:03       26 阅读
  11. 设计模式之模板模式

    2024-07-19 09:00:03       17 阅读