一、导入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.0-beta2</version>
</dependency>
创建实体类映射excel表头
package com.atguigu.yygh.vo.cmn;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@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;
}
二、controller层
@Autowired
private ***tService ***tService;
@PostMapping("/upload")
public R upload(MultipartFile file) throws IOException {
dictService.upload(file);
return R.ok();
}
@GetMapping("/download")
public void download(HttpServletResponse response) throws IOException {
dictService.download(response);
}
三、service层
void download(HttpServletResponse response) throws IOException;
void upload(MultipartFile file) throws IOException;
四、listener层
package com.atguigu.yygh.cmn.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
public class DictListener extends AnalysisEventListener<DictEeVo> {
private DictMapper dictMapper;
public DictListener(DictMapper dictMapper){
this.dictMapper = dictMapper;
}
@Override
public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
Dict dict = new Dict();
BeanUtils.copyProperties(dictEeVo,dict);
QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", dictEeVo.getId());
Integer count = dictMapper.selectCount(queryWrapper);
if (count> 0){
this.dictMapper.updateById(dict);
}else {
this.dictMapper.insert(dict);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
五、serviceImpl层
@Override
public void download(HttpServletResponse response) throws IOException {
List<Dict> list = baseMapper.selectList(null);
List<DictEeVo> dictEeVoList = new ArrayList<>(list.size());
for (Dict dict:list){
DictEeVo dictEeVo = new DictEeVo();
BeanUtils.copyProperties(dict,dictEeVo);
dictEeVoList.add(dictEeVo);
}
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("字典文件", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictEeVoList);
}
@CacheEvict(value = "abc",allEntries = true)
@Override
public void upload(MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet(0).doRead();
}