快速把数据转换成excel

1 篇文章 0 订阅

第一步

maven依赖引入

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

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.2</version>
</dependency>

第二步

创建写对象

@Data
public class ExcelData {
    String erp;
    String deptCpde;
    String info;
}

第三步

构造数据,实际使用的时候,替换成对应查库或者接口

private  List<ExcelData> data() {
    List<ExcelData> list = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        ExcelData data = new ExcelData();
        data.setErp("erp" + i);
        data.setDeptCpde("dept"+i);
        data.setInfo("info"+i);
        list.add(data);
    }
    return list;
}

第四步

调用

    private static void writeExcel(String path){
        //本地测试或者worker应用,获取的是当前模块的classess下
//        String path = new TeamInfoService().getClass().getClassLoader().getResource("/").getPath();
        //web项目,获取tomcat运行根路径,request通过controller传过来,controller的request 来之于@Autowired
        //    protected HttpServletRequest request;
        String path  =  request.getSession().getServletContext().getRealPath("/");
        String fileName = path + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName, ExcelData.class)
                .sheet("模板")
                .doWrite(() -> {
                    // 分页查询数据
                    return data();
                });
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值