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

网友投稿 1007 2022-10-10

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

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

目录一、导出数据字典到Excel1.创建导出实体类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层方法查询数据库,先将查询JXxmUaHQne到的数据放在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 dictList = baseMapper.selectList(null);

// 将Dict转换为DictVo

List 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.页面导出按钮

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

数据导出

4.测试数据导出到Excel

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

二、导入数据字典到网页

1.后台接口代码

Controller层JXxmUaHQne

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 {

// 调用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从网页导入导出数据的演示已经完成,更多关于分布式医疗挂号系统的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Win10 64位 安装VirtualBox管理器 通过CentOS 8 虚拟机安装Linux 8
下一篇:1. 将插入、冒泡排序算法设计成线程,启动两个以上不同的线程同时运行,计算不同排序 的运行时间。
相关文章

 发表评论

暂时没有评论,来抢沙发吧~