当前位置:网站首页>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;
}边栏推荐
- 使用rtknavi进行RT-PPP测试
- [算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
- Fabrication of fairygui simple Backpack
- 堆排序【手写小根堆】
- 音乐播放(Toggle && PlayerPrefs)
- 【无标题】
- MySQL performance tuning - dirty page refresh
- Devops' future: six trends in 2022 and beyond
- Liste des boucles de l'interface graphique de défaillance
- [算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
猜你喜欢
![[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组

idea中好用的快捷键

FGUI工程打包发布&导入Unity&将UI显示出来的方式

堆排序【手写小根堆】

rtklib单点定位spp使用抗差估计遇到的问题及解决
![[算法] 剑指offer2 golang 面试题4:只出现一次的数字](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字

The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
![[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积](/img/e0/cea31070d6365eb57013cdead4a175.png)
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积

FairyGUI条子家族(滚动条,滑动条,进度条)

dosbox第一次使用
随机推荐
FairyGUI增益BUFF数值改变的显示
[899] ordered queue
Excel导入,导出功能实现
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
[algorithm] sword finger offer2 golang interview question 2: binary addition
2021.11.10汇编考试
FairyGUI复选框与进度条的组合使用
Teach you to release a DeNO module hand in hand
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
Latex learning
2021.11.10 compilation examination
燕山大学校园网自动登录问题解决方案
Unity场景跳转及退出
NRF24L01 troubleshooting
NovAtel 板卡OEM617D配置步骤记录
Matlab读取GNSS 观测值o文件代码示例
Database course design: college educational administration management system (including code)
[leetcode19] delete the penultimate node in the linked list
Fairygui joystick
Lock wait timeout exceeded try restarting transaction