广告
返回顶部
首页 > 资讯 > 后端开发 > Python >分布式医疗挂号系统EasyExcel导入导出数据字典的使用
  • 904
分享到

分布式医疗挂号系统EasyExcel导入导出数据字典的使用

2024-04-02 19:04:59 904人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、导出数据字典到excel1.创建导出实体类2.后台接口代码Controller层Service层3.页面导出按钮4.测试数据导出到Excel二、导入数据字典到网页1.后台接口

一、导出数据字典到Excel

1.创建导出实体类

这里导出数据时,只导出网页上每条记录的id、父id、名称、编码、值。

@Data
public class DictEeVo {
    @ExcelProperty(value = "id", index = 0)
    private Long id;
    @ExcelProperty(value = "上级id", index = 1)
    private Long parentId;
    @ExcelProperty(value = "名称", index = 2)
    private String name;
    @ExcelProperty(value = "值", index = 3)
    private String value;
    @ExcelProperty(value = "编码", index = 4)
    private String dictCode;
}

2.后台接口代码

Controller层

为了实现下载数据,Controller层传入httpservletResponse 参数。

    @apiOperation(value = "导出数据字典接口")
    @GetMapping("exportData")
    public void exportDictData(HttpServletResponse response) throws IOException {
        dictService.exportDictData(response);
    }

Service层

Service接口:

void exportDictData(HttpServletResponse response) throws IOException;

Service实现类:

实现类中,首先设置响应类型、响应头、编码等信息。然后通过Dao层方法查询数据库,先将查询到的数据放在dictList集合中,再通过BeanUtils.copyProperties方法将数据放入DictVo中,最后加入dictVoList集合中,传入write方法的参数中。


    @Override
    public void exportDictData(HttpServletResponse response) throws IOException {
        // 设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("数据字典", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
        // 查询数据库
        List<Dict> dictList = baseMapper.selectList(null);
        // 将Dict转换为DictVo
        List<DictVo> dictVoList = new ArrayList<>();
        for (Dict dict : dictList) {
            DictVo dictVo = new DictVo();
            // 将dict中的值复制到dictVo中
            BeanUtils.copyProperties(dict, dictVo);
            dictVoList.add(dictVo);
        }
        // 调用writer方法进行写操作
        EasyExcel.write(response.getOutputStream(), DictVo.class).sheet("数据字典")
                .doWrite(dictVoList);
    }

3.页面导出按钮

页面导出按钮设置了超链接属性,单击后自动调用后端下载接口。

    <a href="http://localhost:8202/admin/cmn/dict/exportData" target="_blank">
      <el-button type="text">
         数据导出
      </el-button>
    </a>

页面导出按钮

4.测试数据导出到Excel

在页面单击 数据导出 按钮后,跳出下载框,成功将页面数据下载到本地.xlsx文件中。

成功将数据下载到本地

二、导入数据字典到网页

1.后台接口代码

Controller层

Controller层通过MultipartFile得到上传的文件。

    @ApiOperation(value = "导入数据字典到网页")
    @PostMapping("importData")
    public Result importDictData(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }

Service层

Service接口

void importDictData(MultipartFile file);

Service实现类

Service中直接使用EasyExcel读取文件中的内容,并加载到数据库

    @Override
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), DictVo.class, new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

配置监听器

监听器中,读取Excel内容到DictVo中,再将DictVO复制到Dict中。最后调用Dao层的方法将DIct添加到数据库。

public class DictListener extends AnalysisEventListener<DictVo> {
    // 调用Dao
    private DictMapper dictMapper;
    public DictListener(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }
    // 读取Excel内容
    @Override
    public void invoke(DictVo DictVo, AnalysisContext context) {
        // 将DictVO对象复制到Dict中
        Dict dict = new Dict();
        BeanUtils.copyProperties(DictVo, dict);
        // 将数据添加到数据库
        dictMapper.insert(dict);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
    }
}

2.页面导入按钮

前端页面导入代码

3.测试数据导入到网页

在Excel中准备两条测试数据:

Excel中准备两条测试数据

将Excel通过页面的 数据导入 按钮上传到数据库:

在页面点击数据导入

成功将Excel中的数据导入数据库,进而通过网页展现:

成功将Excel数据导入

至此,使用EasyExcel从网页导入导出数据的演示已经完成,更多关于分布式医疗挂号系统的资料请关注编程网其它相关文章!

--结束END--

本文标题: 分布式医疗挂号系统EasyExcel导入导出数据字典的使用

本文链接: https://www.lsjlt.com/news/147203.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作