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。

相关推荐
子兮曰20 小时前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰20 小时前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
爱勇宝21 小时前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
胡写代码21 小时前
别再前后端各写一套表单校验了
java·后端
大勇前进1 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端
vipxieliang1 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
yuzhi_liu1 天前
我用 LangGraph4j 实现 Multi-Agent Supervisor
后端
alsmile1 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
大白801 天前
PHP 内存溢出排查思路:看懂报错日志,精准定位问题
后端
二月龙1 天前
PHP 接口返回统一响应封装,让前后端对接更省心
后端