当前位置:网站首页>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;
}边栏推荐
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- Liste des boucles de l'interface graphique de défaillance
- [算法] 剑指offer2 golang 面试题2:二进制加法
- How to improve the deletion speed of sequential class containers?
- [Chongqing Guangdong education] Shandong University College Physics reference materials
- 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
- 最短Hamilton路径 (状压DP)
- Fabrication d'un sac à dos simple fairygui
- [算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
- [Leetcode15]三数之和
猜你喜欢
![[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance

【无标题】

Fairygui loop list

Latex learning

idea中导包方法

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

地球围绕太阳转

(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis

Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
随机推荐
SSD technical features
Lock wait timeout exceeded try restarting transaction
PRIDE-PPPAR源码解析
Mixed use of fairygui button dynamics
[算法] 剑指offer2 golang 面试题2:二进制加法
FairyGUI简单背包的制作
How to reduce the shutdown time of InnoDB database?
First use of dosbox
Matlab读取GNSS 观测值o文件代码示例
In 2020, the average salary of IT industry exceeded 170000, ranking first
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
GNSS定位精度指标计算
VLSM variable length subnet mask partition tips
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
闇の連鎖(LCA+树上差分)
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
[offer29] sorted circular linked list
isEmpty 和 isBlank 的用法区别
FairyGUI循环列表
MySQL shutdown is slow