当前位置:网站首页>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测试
- FairyGUI增益BUFF數值改變的顯示
- Unity scene jump and exit
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- 燕山大学校园网自动登录问题解决方案
- SSD technical features
- MySQL performance tuning - dirty page refresh
- Fabrication d'un sac à dos simple fairygui
- 堆排序【手写小根堆】
- [algorithm] sword finger offer2 golang interview question 2: binary addition
猜你喜欢
编辑距离(多源BFS)
idea中好用的快捷键
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
FairyGUI簡單背包的制作
服务未正常关闭导致端口被占用
【无标题】
Mysql database index
rtklib单点定位spp使用抗差估计遇到的问题及解决
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
随机推荐
【干货】提升RTK模糊度固定率的建议之周跳探测
音乐播放(Toggle && PlayerPrefs)
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
JUC forkjoin and completable future
闇の連鎖(LCA+树上差分)
VLSM variable length subnet mask partition tips
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
【无标题】
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Combination of fairygui check box and progress bar
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
FairyGUI按钮动效的混用
Meanings and differences of PV, UV, IP, VV, CV
Expected value (EV)
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
[offer78] merge multiple ordered linked lists
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组