Java coder懒人福利——Excel导入导出

废话不多说,直接上成品

一、 一行代码导出Excel!
ExcelExportUtil.exportExcel(new ExportParams(),ExcelVO.class,data).write(new FileOutputStream("D:/data.xlsx"));
二、一行代码导入Excel!
List<ExcelVO> data = ExcelImportUtil.importExcel(new File("D:/data.xlsx"), ExcelVO.class, new ImportParams());

详细例子:

一、 先建立好Excel数据图表对应的BO或者VO对象
@Getter
@Setter
public class ExcelVO {

    @Excel(name = "姓名")
    private String name;

    @Excel(name = "年龄")
    private Integer age;
}

对应支持导入的Excel数据表格格式
导入数据模板

具体导入导出代码示例:


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Slf4j
public class PoiDemo {

    public static void main(String[] args) {
        List<ExcelVO> data = ExcelImportUtil.importExcel(new File("D:/data.xlsx"), ExcelVO.class, new ImportParams());
        log.debug("导入的数据集:{}", data); // 将导入的数据输出并且年龄加一模拟数据处理,再导出
        data.forEach(e -> e.setAge(e.getAge() + 1));
        try (FileOutputStream fos = new FileOutputStream("D:/dataNew.xlsx")){
            ExcelExportUtil.exportExcel(new ExportParams(),ExcelVO.class,data).write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

小提示:在try声明处声明输入输出流,可以自动关闭,也可以在finally代码块添加IoUtil.close(closeable);
运行日志

导出的文件里面,年龄字段已经经过了我们的处理再导出,如果是web项目可以输出到response的输出流里面,记得关闭流!记得关闭流!记得关闭流!
在这里插入图片描述
其他具体的详细用法可以查看easypoi官网

最后附上maven依赖,大家可以有选择性的添加依赖

		<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值