欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

easyexcel导出excel模板

程序员文章站 2022-03-15 17:38:02
...

easyexcel导出excel模板

pom文件添加jar依赖

   <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>2.1.4</version>
    </dependency>

java代码

    @GetMapping("export")
    public void export(CivilizedLuckyDrawQuery query, HttpServletResponse response) {
        DataResult<DrawVO> luckyDrawVODataResult = luckyDrawService.queryCivilizedList(query);
        List<DrawVO> data = luckyDrawVODataResult.getData();
        try {
            String fileName = "excel命名_"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            String sheetName = "sheet命名";
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
            EasyExcel.write(response.getOutputStream(), CivilizedLuckyDrawVO.class).sheet(sheetName).doWrite(data);
        } catch (IOException e) {
            log.error("导出异常,e: {}", e);
        }
    }
相关标签: java java