Spring boot集成easy excel实现导入导出操作

Spring boot集成easy excel实现导入导出操作

一 查看官网

easyexcel官方网站地址为easyexcel官网,官网的信息比较齐全,可以查看官网使用easyexcel的功能。

二 引入依赖

使用easyexcel,首先要引入easyexcel的maven依赖,具体的版本根据你的需求去设置。

xml 复制代码
        <!--easyexcel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.10</version>
        </dependency>

三 实现简单导入

首先定义实体类

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Device {
    @ExcelIgnore
    private Integer id;
    @ExcelProperty("设备名称")
    private String name;
    @ExcelProperty("设备编号")
    private String no;
    @ExcelProperty("设备描述")
    private String description;
    @ExcelProperty("设备类型")
    private Integer type;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ExcelIgnore
    private LocalDateTime createTime;
    @ExcelIgnore
    private Integer status;
}

在定义实体类的时候,使用到了lombok,需要提前引入lombok的依赖

xml 复制代码
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

准备工作完成之后,就可以写一个简单的导入了。如下,我在controller中写了导入方法,通过EasyExcel的read方法把excel中的数据解析成对应的列表,然后就可以直接调用service导入了。

java 复制代码
    @RequestMapping("save")
    public String save(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        List<Device> list = EasyExcel.read(file.getInputStream()).head(Device.class).sheet().doReadSync();
        deviceService.batchSave(list);
        return "redirect:/device/lists";
    }

四 实现简单导出

在controller写了简单的导出方法,拿到service得到的数据,就可以直接调用EasyExcel的write方法导出了。

java 复制代码
@GetMapping("export")
    public void export(Dto dto,HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 
        String fileName = URLEncoder.encode("设备数据", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Device> deviceList = deviceService.getDeviceList(dto);
        EasyExcel.write(response.getOutputStream(), Device.class).sheet("数据").doWrite(deviceList);
    }

五 批量导出功能

请参考easyexcel实现批量导出功能

总结

使用easyexcel实现导入和导出确实是非常方便的,同时,easyexcel还支持批量导入和批量导出,确实非常nice。

相关推荐
爱吃山竹的大肚肚15 分钟前
优化SQL:如何使用 EXPLAIN
java·数据库·spring boot·sql·spring
Tony Bai26 分钟前
Go 性能分析的“新范式”:用关键路径分析破解高并发延迟谜题
开发语言·后端·golang
Kiyra28 分钟前
Spring Boot Starter 自定义开发:封装中间件配置
spring boot·redis·后端·缓存·中间件·性能优化·rocketmq
HABuo29 分钟前
【Linux进程(一)】进程深入剖析-->进程概念&PCB的底层理解
linux·运维·服务器·c语言·c++·后端·进程
Teable任意门互动37 分钟前
从飞书多维表格 简道云到Teable多维表格:企业为何选择Teable作为新一代智能数据协作平台?
数据库·excel·钉钉·飞书·开源软件
码界奇点1 小时前
基于Spring Boot和微信小程序的小程序商城系统设计与实现
spring boot·微信小程序·小程序·毕业设计·源代码管理
武藤一雄1 小时前
C# 中线程安全都有哪些
后端·安全·微软·c#·.net·.netcore·线程
+VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue英语学习系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
步步为营DotNet1 小时前
深度剖析ASP.NET Core Middleware:构建高效请求处理管道的关键
后端·asp.net