//返回文件的格式;比如html;现在是设置返回excel类型
response.setContentType(;application/vnd.ms-excel;);
response.setCharacterEncoding(;utf-8;);
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
response.setHeader(;Content-disposition;, ;attachment;filename=; ; fileName ; ;.xlsx;);
//内容样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);//细实线
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设计内容居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); //设置内容自动换行
contentWriteCellStyle.setWrapped(true);
//设置头部样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置头部标题居中
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
//将数据写进流;如果是响应流则写进header对应的文件里
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
//指定sheet的名字和头信息
WriteSheet writeSheet = EasyExcel.writerSheet(0, ;日历导出;).head(getHead(calendarExportVO.getTitle())).registerWriteHandler(new SimpleColumnWidthStyleStrategy(19)).registerWriteHandler(horizontalCellStyleStrategy).build();
//将数据真正写入excel中
excelWriter.write(calendarExportVO.getData(), writeSheet);
//千万别忘记finish 会帮忙关闭流
excelWriter.finish();