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

avatar
作者
筋斗云
阅读量:1

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);     } }  

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!