1. EasyExcel export cell content is too long to cover other cells
As shown in the figure below:
Expectation: Export and open it should be like this
Two, the code
/**
* 导出Excel
*/
@GetMapping("/export")
@RequestLog("随访记录信息导出Excel")
@ApiOperation(value = "随访记录信息导出", notes = "请求头需携带有效token")
public void export(FollowUpRecordReq req, HttpServletResponse response) {
final List<FollowUpRecordPageRep> list = followUpRecordService.export(req);
try (final ServletOutputStream os = response.getOutputStream()) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream;charset=UTF-8");
final String fileName = String.format("随访记录-%s.xlsx", DateTime.now().toString("yyyyMMddHHmm"));
final String encodeFileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", String.format("attachment;filename=%s", encodeFileName));
//设置单元格格式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//设置 自动换行不开启
// contentWriteCellStyle.setWrapped(true);
//设置 水平对齐方式为常规
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.GENERAL);
//设置 垂直对齐方式为靠下
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(null,contentWriteCellStyle);
EasyExcel.write(os, FollowUpRecordPageRep.class)
.registerWriteHandler(ExcelStyleUtils.getCellStyle())
// .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.registerWriteHandler(horizontalCellStyleStrategy)
.sheet("随访记录")
.doWrite(list);
} catch (Exception e) {
log.error("导出随访记录失败", e);
throw new BizException("导出随访记录失败", e);
}
}