1、单元格数据样例:
你好\r\n大家好\r\n
2、增加单元格拦截器
/*** excel换行符处理*/
@Slf4j
public class WrapCellWriteHandler implements CellWriteHandler {@Overridepublic void afterCellDispose(CellWriteHandlerContext context) {Cell cell = context.getCell();Row row = cell.getRow();String value = cell.getStringCellValue();if (value.contains("\r\n")) {context.getFirstCellData().getOrCreateStyle().setWrapped(true);int length = Math.max(value.split("\r\n")[0].length(), 10);int headLength = Math.max(context.getWriteSheetHolder().getSheet().getRow(0).getCell(cell.getColumnIndex()).getStringCellValue().length(), 10);cell.getSheet().setColumnWidth(cell.getColumnIndex(), Math.max(length, headLength) * 256);row.setHeight((short) (value.split("\r\n").length * 256));}}
}
3、注册
EasyExcel.write(fileName).registerWriteHandler(new WrapCellWriteHandler());