Open jakesang opened 5 months ago
public class WrapTextWriteHandler extends AbstractCellWriteHandler {
private CellStyle cellStyle;
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
Cell cell = context.getCell();
if (BooleanUtils.isNotTrue(context.getHead())) {
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
CellStyle cellStyle = getCellStyle(workbook);
cell.setCellStyle(cellStyle);
context.getFirstCellData().setWriteCellStyle(null);
}
}
public CellStyle getCellStyle(Workbook workbook) {
if (cellStyle == null) {
cellStyle = workbook.createCellStyle();
cellStyle.setWrapText(true);
}
return cellStyle;
}
}
异常代码
问题描述
1、这段代码是重写了LongestMatchColumnWidthStyleStrategy方法 2、为了自定义最大宽度,且当达到最大宽度时,单元格设置自动换行 3、debug调试过,确定到了具体的cell是执行了cellStyle.setWrapText(true); 4、实际下载的excel中变成表头有自动换行,目标单元格没有