栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java+easypoi 简单模板导出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java+easypoi 简单模板导出

Java+easypoi 简单模板导出

1.新建一个springboot项目

2.pom.xml

        
            cn.afterturn
            easypoi-base
            4.1.0
            
                
                    guava
                    com.google.guava
                
            
        
        
            cn.afterturn
            easypoi-web
            4.1.0 
        
            cn.afterturn
            easypoi-annotation
            4.1.0
        

3.新建需要导出的实体类

import lombok.Data;


@Data
public class Student {

    private String id;

    private String name;
}

4.新建一个studentList.xls表格模板

5.将模板放入项目中,放在目录 template/ 下

6.新建controller

@RestController
public class ProviderController {
    @PostMapping("/exportToExcel")
    public void exportToExcel(HttpServletResponse response) {
        List list =new ArrayList<>();
        Student student=new Student();
        Student student2=new Student();
        student.setId("1001");
        student.setName("狄欧苏");
        student2.setId("1002");
        student2.setName("酸豆角了");
        list.add(student);
        list.add(student2);
        
        String TemplateName="studentList";
        TemplateExportParams templateExportParams = new TemplateExportParams("template/"+TemplateName+".xls");
        Map map = new HashMap<>(8);
        map.put("list", list);
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, map);
        // 设置请求头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = TemplateName+System.currentTimeMillis()+".xlsx";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        try (ServletOutputStream outputStream = response.getOutputStream()){
            workbook.write(outputStream);
        } catch (IOException e) {
            log.warn("导出失败: {}", e.getMessage());
        }
    }
}

7.运行项目

8.使用postman发送post请求

9.导出成功



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号