SpringBoot学习笔记-----整合EasyExcel

maven 引入依赖

1
2
3
4
5
xml复制代码<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>

导出数据

controller层

1
2
3
4
5
6
7
8
9
java复制代码
@Autowired
private DictService dictService;

@ApiOperation(value = "字典表数据的导出")
@GetMapping(value = "exportData")
public void exportData(HttpServletResponse response){
dictService.exportData(response);
}

service层:这里只给了 实现类里面的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
java复制代码/**
* 字段数据 导出成 excel
* @param response
*/
@Override
public void exportData(HttpServletResponse response) {


try {
//设置返回的数据格式
response.setContentType("application/vnd.ms-excel");
//设置返回的数据编码
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("数据字典", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

//从数据空中 查询字典数据列表
//dict代表数据库表的映射实体类 dictEeVo代表excel数据映射的实体类
//现在查询到设置的泛型是 dict 需要将数据列表的泛型变成dictEeVo 才能向Excel文件中写入数据
List<Dict> dictList = baseMapper.selectList(null);

List<DictEeVo> dictEeVoList = new ArrayList<>(dictList.size());

for(Dict dict : dictList) {
DictEeVo dictVo = new DictEeVo();

//将一个实体类的数据 复制到另一个实体类 【查出来映射的是数据库表的实体类 现在要映射成Excel表的实体类】
BeanUtils.copyProperties(dict,dictVo);
dictEeVoList.add(dictVo);
}
//excel写入数据 输出流 excel实体类映射 导出的模板名称
EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("数据字典")
//要导出的数据列表
.doWrite(dictEeVoList);

} catch (IOException e) {
e.printStackTrace();
}
}

mapper层的查询数据 自己随便写:导出时的场景

image.png

导入数据

实现读取监听器 DictListener:读取excel文件的数据以后 通过监听器向数据表中一行一行的插入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
java复制代码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.model.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DictListener extends AnalysisEventListener<DictEeVo> {

@Autowired
private DictMapper dictMapper;

//一行一行的读取数据
@Override
public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {

//dict对应数据库表的实体类 dictEeVo代表Excel数据映射的实体类
Dict dict = new Dict();
//需要将获取的 Excel实体类的数据 变成数据库表映射的实体类数据 才能插入到数据库
BeanUtils.copyProperties(dictEeVo,dict);
//设置是否删除 当前这一行是我个人的业务
dict.setIsDeleted(0);
//一行一行的插入数据
dictMapper.insert(dict);
}

//导出数据完成调用
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("导入数据完成");
}
}

service层代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java复制代码
@Autowired
private DictListener dictListener;


/**
* 字典数据的导入
* @param file
*/
@Override
public void importDictData(MultipartFile file) {
try {
//从excel中读取数据 输入流 excel实体类 监听器将数据插入到表
EasyExcel.read(file.getInputStream(),DictEeVo.class,dictListener).sheet().doRead();
} catch (IOException e) {
e.printStackTrace();
}
}

controller层代码

1
2
3
4
5
6
java复制代码@ApiOperation(value = "字典表数据导入")
@PostMapping("importData")
public R importData(MultipartFile file){
dictService.importDictData(file);
return R.ok();
}

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%