当前位置:网站首页>Excel导入,导出功能实现
Excel导入,导出功能实现
2022-07-06 09:18:00 【Demon Lord(大魔王)】
日常在做后台系统的时候会很频繁的遇到Excel导入导出的问题,下面就流程写出
1.依赖 alibaba的easyexcel
<groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId>
2.注解

3.导入 isCovered是是否覆盖的参数,不需要这个功能可以忽略
@PostMapping("/import-dept")
@ApiOperationSupport(order = 13)
@ApiOperation(value = "导入部门", notes = "传入excel")
public void importDept(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "isCovered") String isCovered) throws IOException {
deptService.importDept(file ,isCovered);
}Service层
@Transactional(rollbackFor = Exception.class)
@Override
public void importDept(MultipartFile file, String isCovered) throws IOException {
//取流
InputStream inputStream = file.getInputStream();
//转为定义的excel实体集合-根据部门分组
List<DeptExcel> deptExcelList = EasyExcel.read(inputStream).head(DeptExcel.class)
// 设置sheet,默认读取第一个
.sheet()
// 设置标题所在行数
.headRowNumber(1)
.doReadSync();
List<Dept> deptList = new ArrayList<>();
//循环字段翻译,就忽略了
deptExcelList.forEach(deptExcel -> {
Dept dept1 = new Dept();
if (StringUtil.isNotBlank(deptExcel.getParentname())) {
if (Objects.nonNull(DeptCache.getDeptByName(deptExcel.getParentname()))) {
dept1.setParentId(DeptCache.getDeptByName(deptExcel.getParentname()).getId());
}
}
dept1.setVcode(deptExcel.getVcode());
dept1.setVname(deptExcel.getVname());
deptList.add(dept1);
});
if (isCovered.equals("0")) {
deptList.forEach(dept -> {
Long id = this.getOne(Wrappers.<Dept>query().lambda().eq(Dept::getVcode, dept.getVcode())).getId();
if (id != null) {
dept.setId(id);
this.saveOrUpdate(dept);
} else {
this.saveOrUpdate(dept);
}
});
} else {
deptList.forEach(this::saveOrUpdate);
}
}4.导出
/**
* 导出部门
*/
@GetMapping("/export-dept")
@ApiOperationSupport(order = 14)
@ApiOperation(value = "导出部门", notes = "传入user")
public void exportDept(@ApiIgnore @RequestParam Map<String, Object> dept, HttpServletResponse response) {
QueryWrapper<Dept> queryWrapper = Condition.getQueryWrapper(dept, Dept.class);
List<DeptExcel> list = deptService.exportDept(queryWrapper);
ExcelUtil.export(response, "导出部门" + DateUtil.time(), "导出部门表", list, DeptExcel.class);
}
//写的工具类 就不全发出 自己拼进去
private static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(Charsets.UTF_8.name());
fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(dataList);
} catch (Throwable var6) {
throw var6;
}
}Service层
@Override
public List<DeptExcel> exportDept(QueryWrapper<Dept> queryWrapper) {
List<Dept> deptExcels = this.list(queryWrapper);
List<DeptExcel> deptExcelList = new ArrayList<>();
//翻译部分略过
deptExcels.forEach(dept1 -> {
DeptExcel deptExcel = new DeptExcel();
if (dept1.getParentId() != null) {
if (Objects.nonNull(DeptCache.getDept(dept1.getParentId()))) {
deptExcel.setParentname(DeptCache.getDept(dept1.getParentId()).getVname());
}
}
deptExcel.setVname(dept1.getVname());
deptExcel.setVcode(dept1.getVcode());
deptExcelList.add(deptExcel);
});
return deptExcelList;
}边栏推荐
- 2021.11.10 compilation examination
- [offer9] implement queues with two stacks
- How to add music playback function to Arduino project
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
- Expected value (EV)
- Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
- [Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
- Knowledge system of digital IT practitioners | software development methods -- agile
- Lock wait timeout exceeded try restarting transaction
- 單片機藍牙無線燒錄
猜你喜欢

Force buckle 1189 Maximum number of "balloons"

Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)

FairyGUI增益BUFF數值改變的顯示

Learning notes of JS variable scope and function

Fabrication d'un sac à dos simple fairygui

idea中导包方法

音乐播放(Toggle && PlayerPrefs)

2021.11.10 compilation examination

RTKLIB: demo5 b34f.1 vs b33

(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
随机推荐
Fabrication d'un sac à dos simple fairygui
CUDA C programming authoritative guide Grossman Chapter 4 global memory
VLSM variable length subnet mask partition tips
2021.11.10汇编考试
Unity3D制作注册登录界面,并实现场景跳转
單片機藍牙無線燒錄
MySQL performance tuning - dirty page refresh
Guided package method in idea
What is the maximum length of MySQL varchar field
Special palindromes of daily practice of Blue Bridge Cup
Solution to the problem of automatic login in Yanshan University Campus Network
Fabrication of fairygui simple Backpack
(五)R语言入门生物信息学——ORF和序列分析
[offer9]用两个栈实现队列
KF UD分解之UD分解基础篇【1】
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Unity3d, Alibaba cloud server, platform configuration
Redis based distributed ID generator
@The difference between Autowired and @resource
idea问题记录