SpringBoot中EasyExcel相关使用

该博客介绍了如何在SpringBoot项目中结合MybatisPlus使用EasyExcel进行数据字典的导入与导出。通过创建DictEeVo和Dict实体类,配置相关依赖,并在Controller、Service及监听器中编写方法,实现了从Excel表格导入数据字典到数据库,以及将数据库中的数据字典导出到Excel文件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

EasyExcel相关使用

项目基于springBoot、MybatisPlus上开发

导入相关依赖,创建DictEeVo和Dict实体类

DictEeVo对应excel表格中的字段
Dict对应数据库中的字段

		<dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
            <version>2.1.1</version>
        </dependency>

在controller类中,编写相关方法
ApiOperation为Swagger2中的注解

    @Autowired
    private IDictService dictService;

    //数据字典 导出
    @ApiOperation(value="导出")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) throws UnsupportedEncodingException {
            dictService.exportData(response);
    }

    //数据字典 导入
    @ApiOperation(value="导入")
    @PostMapping("importData")
    public void importData(MultipartFile file){
        dictService.importData(file);
    }

使用导出功能,需要创建一个监听器,在invok中定义要输出要执行的方法

public class DictListener extends AnalysisEventListener<DictEeVo> {

    private DictMapper dictMapper;
    //使用构造器,调用dictMapper
    public DictListener(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }

    //一行一行读取
    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext context) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictEeVo,dict);
        dictMapper.insert(dict);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
}

在Iservice中定义方法接口

public interface IDictService extends IService<Dict> {

    //导出数据字典
    void exportData(HttpServletResponse response) throws UnsupportedEncodingException;

    //导入数字字典
    void importData(MultipartFile file);
}

创建一个实现类,实现接口

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Autowired
    private DictMapper dictMapper;

    //导出数据字典
    @Override
    public void exportData(HttpServletResponse response) throws UnsupportedEncodingException {
        //获取文件上传流
        // setContentType:设置发送到客户端的响应的内容类型,此时响应还没有提交。给出的内容类型可以包括字符编码说明,报文头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("数据字典", "UTF-8");
        /**
         *  setHeader:用给定的名称和值设置响应头,要求客户端进行认证的方式,请求的资源已移动到新地址
         * Content-disposition: 会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名
         * */
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据字典信息
        List<Dict> dictsList = dictMapper.selectList(null);
        //导出excel对应字段实体类
        List<DictEeVo> dictEeVos = new ArrayList<>();
        //将dict --> dictEeVo
        for (Dict dict : dictsList) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVos.add(dictEeVo);
        }
        //调用easyExcel导出excel文件,write(流方式,字段表类)
        try {
            EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet().doWrite(dictEeVos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //导入数据字典
    @Override
    public void importData(MultipartFile file) {
        try {
            //调用easyExcel导入excel文件,插入到数据库中
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(dictMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //判断节点是否含有子节点,设置hasChildren属性
    private boolean isChildren(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper();
        wrapper.eq("parent_id", id);
        Integer count = dictMapper.selectCount(wrapper);
        return count > 0;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值