官方文档地址:
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);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix