Springboot 使用easyexcel 实现Excel导出
导入easyexcel依赖
<!--alibaba easyexcel excel导出依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.3</version>
</dependency>
封装类上面的 @ExcelProperty(value = “设备号”, index = 0)
是excel的列名 和位置
代码
@ExcelProperty(value = "设备号", index = 0)
导出excel 控制层
/**历史任务 EXCEL导出功能
* 参数 模糊查询 封装类
* */
@RequestMapping("/downloadEasyExcel")
public void downloadEasyExcel(HttpServletResponse response,Task task) throws IOException {
String fileName = URLEncoder.encode("历史任务列表", "UTF-8") + ConFig.newDate();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
// excel头策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 11);
headWriteFont.setBold(false);
headWriteCellStyle.setWriteFont(headWriteFont);
// excel内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short)11);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 设置handler
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
/** 导出数据查询方法*/
List<TaskExcel> tasks = taskService.selectHistoricalMissionEXCEL(task);
EasyExcel.write(response.getOutputStream(), TaskExcel.class)
.sheet("下载excel服务")
.registerWriteHandler(styleStrategy)
.doWrite(tasks);
}
在导出excel的时候经常会有这种需求 数据库里面存放的是数字 但是导出excel的时候不能显示数字
在sql使用 CASE方法
代码
(CASE task_status WHEN '3' THEN '完成' end ) as task_status,
最后导出样式