使用 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);
}
}
原文地址:https://blog.csdn.net/weixin_46085253/article/details/140487952
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!