官方文档地址:
https://easyexcel.opensource.alibaba.com/docs/current/
主代码:
try {
XsxExportTitleVO xsxExportTitleVO = new XsxExportTitleVO();
xsxExportTitleVO.setTitle(titleExcel);
String fileName = UUID.randomUUID().toString();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
ClassPathResource couponOrderTemplateResource = new ClassPathResource("template/xsxExport.xlsx");
ServletOutputStream outputStream = response.getOutputStream();
ExcelWriter excelWriter = EasyExcel
.write(outputStream)
.withTemplate(couponOrderTemplateResource.getInputStream())
.registerWriteHandler(new CustomCellWriteHandler())
.inMemory(true)
.build();
response.setContentType("multipart/form-data");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
//创建第一个sheet
WriteSheet sumEvaSheet = EasyExcel.writerSheet("课程表").build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
// 标题
excelWriter.fill(new FillWrapper("title", Arrays.asList(xsxExportTitleVO)), sumEvaSheet);
excelWriter.fill(new FillWrapper("lesson", xsxExportVOList), fillConfig, sumEvaSheet);
excelWriter.finish();
} catch (Exception e) {

}


事件监听处理代码:
用来处理同一单元格中不同的样式
public class CustomCellWriteHandler implements CellWriteHandler {
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
Cell cell = context.getCell();
int cellIndex = cell.getColumnIndex();
int rowIndex = cell.getRowIndex();
Font writeFont = new XSSFFont();
writeFont.setBold(true);
writeFont.setFontHeightInPoints((short) 12);
writeFont.setFontName("等线");

Font font = new XSSFFont();
font.setFontName("等线");
// 内容
String cellValue = cell.getStringCellValue();
if (!StringUtils.isEmpty(cellValue)) {
if (cellIndex >= 1 && cellIndex <= 7 && rowIndex >= 2 && rowIndex <= 4) {
RichTextString richTextString = new XSSFRichTextString(cellValue);
richTextString.applyFont(0, cellValue.length() - 1, font);
int lessonIndex = cellValue.indexOf("课程:");
while (lessonIndex >= 0) {
richTextString.applyFont(lessonIndex, lessonIndex + 3, writeFont);
lessonIndex = cellValue.indexOf("课程:", lessonIndex + 3);
}
int classNameIndex = cellValue.indexOf("班级:");
while (classNameIndex >= 0) {
richTextString.applyFont(classNameIndex, classNameIndex + 3, writeFont);
classNameIndex = cellValue.indexOf("班级:", classNameIndex + 3);
}
int teacherIndex = cellValue.indexOf("教师:");
while (teacherIndex >= 0) {
richTextString.applyFont(teacherIndex, teacherIndex + 3, writeFont);
teacherIndex = cellValue.indexOf("教师:", teacherIndex + 3);
}
int roomIndex = cellValue.indexOf("教室:");
while (roomIndex >= 0) {
richTextString.applyFont(roomIndex, roomIndex + 3, writeFont);
roomIndex = cellValue.indexOf("教室:", roomIndex + 3);
}
cell.setCellValue(richTextString);

}
}
}
}


posted on 2024-01-16 11:09  田坤坤  阅读(7)  评论(0编辑  收藏  举报