当前位置:网站首页>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;
}边栏推荐
- Unity3D制作注册登录界面,并实现场景跳转
- 第一人称视角的角色移动
- [leetcode622] design circular queue
- [leetcode15] sum of three numbers
- FairyGUI按钮动效的混用
- (the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
- Minio file download problem - inputstream:closed
- Conditional probability
- [算法] 剑指offer2 golang 面试题1:整数除法
- (core focus of software engineering review) Chapter V detailed design exercises
猜你喜欢

NovAtel 板卡OEM617D配置步骤记录

Fairygui loop list
![[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组

Derivation of logistic regression theory

2021.11.10汇编考试

Database course design: college educational administration management system (including code)
![[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once

【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现

Fairygui character status Popup
![[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
随机推荐
Meanings and differences of PV, UV, IP, VV, CV
C programming exercise
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Fairygui gain buff value change display
Fabrication d'un sac à dos simple fairygui
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
基本Dos命令
闇の連鎖(LCA+树上差分)
FairyGUI条子家族(滚动条,滑动条,进度条)
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
[offer18] delete the node of the linked list
NovAtel 板卡OEM617D配置步骤记录
Programming homework: educational administration management system (C language)
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
wsl常用命令
isEmpty 和 isBlank 的用法区别
GPS高程拟合抗差中误差的求取代码实现
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
Servlet