先引入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
导出
@Override
public void exportDictData(HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("数据字典", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
List<Dict> dictList = dictMapper.selectList(null);
List<DictEeVo> dictEeVoList=new ArrayList<>();
for (Dict dict:dictList){
DictEeVo dictEeVo=new DictEeVo();
dictEeVo.setId(dict.getId());
BeanUtils.copyProperties(dict,dictEeVo);
dictEeVoList.add(dictEeVo);
}
EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictEeVoList);
}catch (IOException e){
e.printStackTrace();
}
导入
@Override
public void importDictData(MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(dictMapper)).sheet().doRead();
}catch (IOException e){
e.printStackTrace();
}
}
public class DictListener extends AnalysisEventListener<DictEeVo> {
private DictMapper dictMapper;
public DictListener(DictMapper dictMapper){
this.dictMapper=dictMapper;
}
@Override
public void invoke(DictEeVo data, AnalysisContext context) {
Dict dict=new Dict();
BeanUtils.copyProperties(data,dict);
dictMapper.insert(dict);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
}
|