当前位置:网站首页>Implementation of Excel import and export functions
Implementation of Excel import and export functions
2022-07-06 12:52:00 【Demon Lord】
I often encounter when I am working on the background system Excel Import and export problems , Here is the process
1. rely on alibaba Of easyexcel
<groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId>
2. annotation
3. Import isCovered Is the parameter of whether to overwrite , This function is not required and can be ignored
@PostMapping("/import-dept")
@ApiOperationSupport(order = 13)
@ApiOperation(value = " Import Department ", notes = " Pass in excel")
public void importDept(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "isCovered") String isCovered) throws IOException {
deptService.importDept(file ,isCovered);
}
Service layer
@Transactional(rollbackFor = Exception.class)
@Override
public void importDept(MultipartFile file, String isCovered) throws IOException {
// Take current
InputStream inputStream = file.getInputStream();
// Turn to defined excel Set of entities - Group by Department
List<DeptExcel> deptExcelList = EasyExcel.read(inputStream).head(DeptExcel.class)
// Set up sheet, The first one is read by default
.sheet()
// Set the number of rows in the title
.headRowNumber(1)
.doReadSync();
List<Dept> deptList = new ArrayList<>();
// Loop field translation , Just ignore
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. export
/**
* Export Department
*/
@GetMapping("/export-dept")
@ApiOperationSupport(order = 14)
@ApiOperation(value = " Export Department ", notes = " Pass in 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, " Export Department " + DateUtil.time(), " Export department table ", list, DeptExcel.class);
}
// Write tool class Not all of them Put it in by yourself
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 layer
@Override
public List<DeptExcel> exportDept(QueryWrapper<Dept> queryWrapper) {
List<Dept> deptExcels = this.list(queryWrapper);
List<DeptExcel> deptExcelList = new ArrayList<>();
// The translation part is omitted
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;
}
边栏推荐
- [leetcode19] delete the penultimate node in the linked list
- dosbox第一次使用
- Fairygui loop list
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- On March 15, the official version of go 1.18 was released to learn about the latest features and usage
- Easy to use shortcut keys in idea
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- How to improve the deletion speed of sequential class containers?
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- Naive Bayesian theory derivation
猜你喜欢
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
First use of dosbox
[algorithm] sword finger offer2 golang interview question 2: binary addition
[Chongqing Guangdong education] Shandong University College Physics reference materials
Expected value (EV)
FairyGUI复选框与进度条的组合使用
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
微信小程序开发心得
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
Force buckle 1189 Maximum number of "balloons"
随机推荐
SVN更新后不出现红色感叹号
Agile development helps me
Fairygui character status Popup
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
idea问题记录
wsl常用命令
Unity场景跳转及退出
服务未正常关闭导致端口被占用
2022国赛Re1 baby_tree
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
C programming exercise
【无标题】
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Naive Bayesian theory derivation
Database course design: college educational administration management system (including code)
Acwing-116 pilot brother
There is no red exclamation mark after SVN update
Liste des boucles de l'interface graphique de défaillance
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers