Springboot uses easyexcel to implement Excel export function

Springboot uses easyexcel to implement Excel export

Import easyexcel dependencies

 <!--alibaba easyexcel  excel导出依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.3</version>
        </dependency>

@ExcelProperty(value = "device number", index = 0) above the package class
is the column name and location
Insert picture description herecode of excel

 @ExcelProperty(value = "设备号", index = 0)

Export the excel control layer

/**历史任务  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);
        }

When exporting excel, there is often such a requirement. The database stores numbers but the numbers cannot be displayed when exporting excel.
Insert picture description here

Use the CASE method
Insert picture description here
code in SQL

(CASE task_status WHEN '3' THEN '完成' end ) as task_status,

Finally export the style
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44664329/article/details/109355716