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。

相关推荐
IT_陈寒1 小时前
JavaScript性能飞跃:5个V8引擎优化技巧让你的代码提速300%
前端·人工智能·后端
Victor3561 小时前
Redis(61)Redis的连接数上限是多少?
后端
Victor3561 小时前
Redis(60) Redis的复制延迟如何优化?
后端
曾令胜7 小时前
excel导出使用arthas动态追踪方法调用耗时后性能优化的过程
spring·性能优化·excel
.格子衫.7 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多7 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
摇滚侠9 小时前
Spring Boot 3零基础教程,Spring Intializer,笔记05
spring boot·笔记·spring
Jabes.yang9 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
兮动人9 小时前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
我命由我1234510 小时前
Excel - Excel 列出一列中所有不重复数据
经验分享·学习·职场和发展·word·powerpoint·excel·职场发展