java.lang.IllegalArgumentException: The maximum length of cell contents (text) is 32,767 characters
/** * sqlText 字段导出 excel 时的转化器 */ public class SqlTextConverter implements Converter { @Override public WriteCellData> convertToExcelData(WriteConverterContext context) throws Exception { // 截取 SQL 语句前 32767 个字符 WriteCellData 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;
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); } }