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。

相关推荐
zzb15803 小时前
项目集成Spring Security认证部分
java·后端·spring
码农小旋风8 小时前
Hive分区和分桶
后端
轩情吖9 小时前
二叉树-堆(补充)
c语言·数据结构·c++·后端·二叉树··排序
SomeB1oody9 小时前
【Rust自学】19.2. 高级trait:关联类型、默认泛型参数和运算符重载、完全限定语法、supertrait和newtype
开发语言·后端·rust
武昌库里写JAVA10 小时前
redis原理之数据结构
spring boot·spring·毕业设计·layui·课程设计
加油,旭杏11 小时前
【go语言】函数
开发语言·后端·golang
2501_9032386512 小时前
自定义登录页面的Spring Security实践
java·后端·spring·个人开发
飞翔的佩奇13 小时前
Java项目: 基于SpringBoot+mybatis+maven+mysql实现的图书管理系统(含源码+数据库+答辩PPT+毕业论文)
java·数据库·spring boot·mysql·spring·毕业设计·图书管理
一 乐14 小时前
基于vue船运物流管理系统设计与实现(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端·船运系统
沈韶珺15 小时前
Elixir语言的安全开发
开发语言·后端·golang