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。

相关推荐
皮皮林5514 小时前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
weixin_456904275 小时前
Spring Boot 用户管理系统
java·spring boot·后端
奔跑吧邓邓子5 小时前
【Java实战㉞】从0到1:Spring Boot Web开发与接口设计实战
java·spring boot·实战·web开发·接口设计
茶本无香5 小时前
深入理解Spring Boot的EnvironmentPostProcessor:环境处理的黑科技
spring boot
奔跑吧邓邓子5 小时前
【Java实战㉝】Spring Boot实战:从入门到自动配置的进阶之路
java·spring boot·实战·自动配置
ONLYOFFICE5 小时前
【技术教程】如何将ONLYOFFICE文档集成到使用Spring Boot框架编写的Java Web应用程序中
java·spring boot·编辑器
cyforkk6 小时前
Spring 异常处理器:从混乱到有序,优雅处理所有异常
java·后端·spring·mvc
程序员爱钓鱼6 小时前
Go语言实战案例-开发一个Markdown转HTML工具
前端·后端·go
桦说编程7 小时前
爆赞!完全认同!《软件设计的哲学》这本书深得我心
后端