信息发布→ 登录 注册 退出

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

发布时间:2026-01-11

点击量:
目录
  • 一、导出数据字典到Excel
    • 1.创建导出实体类
    • 2.后台接口代码
      • Controller层
      • Service层
    • 3.页面导出按钮
      • 4.测试数据导出到Excel
      • 二、导入数据字典到网页
        • 1.后台接口代码
          • Controller层
          • Service层
          • 配置监听器
        • 2.页面导入按钮
          • 3.测试数据导入到网页

          一、导出数据字典到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方法的参数中。

          /**
               * 导出数据字典接口
               * @param response
               */
              @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中的数据导入数据库,进而通过网页展现:

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

          在线客服
          服务热线

          服务热线

          4008888355

          微信咨询
          二维码
          返回顶部
          ×二维码

          截屏,微信识别二维码

          打开微信

          微信号已复制,请打开微信添加咨询详情!