赞
踩
有这样的需求,需要批量新增数据,但是一个一个的表单填写太麻烦,可以通过excel将数据汇总,然后通过上传的方式显示出来,在前端页面进行勾选,实现批量上传,需要实现一下功能:
@PostMapping(value = "/upload/url/excel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(value = HttpStatus.OK)
@PreAuthorize("hasAnyRole('ADMIN')")
public Flux<SysApi> uploadUrl(@RequestPart("file") Flux<FilePart> filePart){
return ioService.upload(filePart, SysApi.class)
.cast(SysApi.class);
}
@PostMapping("/download/url/excel")
@PreAuthorize("hasAnyRole('ADMIN', 'IT', 'HR')")
public Mono<Void> downloadUrl(ServerHttpResponse response) {
return sysApiService.findAll()
.collectList()
.flatMap(objs-> {
try {
return ioService.downloadFromDb(objs, response, SysApi.class);
} catch (UnsupportedEncodingException e) {
return Mono.error(new UnsupportedEncodingException());
}
});
}
case "java.util.List":
value = Arrays.stream(str.substring(1, str.length() - 1).split(","))
.map(String::trim)
.collect(Collectors.toList());
case BLANK:
case _NONE:
if (type.getTypeName().equals("java.util.List")) value = new ArrayList<>();
break;
public Mono<Void> downloadFromDb(List<?> objs, ServerHttpResponse response, Class<? extends DataChange> clazz) throws UnsupportedEncodingException { String fileName = new String(("test" + LocalDateTime.now().toLocalDate() + ".xlsx").getBytes(StandardCharsets.UTF_8), "iso8859-1"); File file = new File(fileName); Set<String> banSet = Stream.of("createBy", "createTime", "lastUpdateBy", "lastUpdateTime", "roles", "password", "frozen").collect(Collectors.toSet()); return isAdmin() .flatMap(isAdmin -> { List<String> header = Stream.of(clazz.getDeclaredFields()) .map(Field::getName) .filter(it-> isAdmin || !banSet.contains(it)) .collect(Collectors.toList()); return WriteExcelUtil.data2Workbook(objs, clazz, header); }) .flatMap(workbook -> { try { workbook.write(new FileOutputStream(file)); } catch (IOException e) { e.printStackTrace(); } return downloadFile(response, file, fileName); }); }
ReactiveSecurityContextHolder
获取权限信息,然后判断是否为ADMIN用户private Mono<Boolean> isAdmin() {
return ReactiveSecurityContextHolder.getContext()
.map(it-> it.getAuthentication()
.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toSet())
)
.map(it -> it.contains("ROLE_ADMIN"));
}
access.canAdmin && radioValue==='upload' &&(
<Upload {...uploadProps}
>
<Button type="primary">
<UploadOutlined /> 上传
</Button>
</Upload>
),
radio
切换查询和上传功能onChange
更换value,通过hook进行数据切换{access.canAdmin&&(<Radio.Group
defaultValue="read"
buttonStyle="solid"
onChange={(e) => {
setRadioValue(e.target.value)
// @ts-ignore
actionRef.current?.reloadAndRest();
}}
>
<Radio.Button value="read">查询</Radio.Button>
<Radio.Button value="upload">上传</Radio.Button>
</Radio.Group>)}
table
通过radio
的value更换切换数据来源request={()=>{
if (radioValue === 'upload'){
return getApiData();
}
return findAllApi();
}}
onChange
将上传返回数据通过hook设置onChange(info:any) {
if (info.file.status !== 'uploading') {
// console.log(info.file, info.fileList);
}
if (info.file.status === 'done') {
setApiData(info.file.response);
// @ts-ignore
actionRef.current?.reloadAndRest();
message.success(`${info.file.name} file uploaded successfully`);
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
{access.canAdmin && radioValue === 'upload'&&(<Button
type="primary"
onClick={async () => {
await handleUpdateMany(selectedRowsState);
setSelectedRows([]);
}}
>
批量新增
</Button>)}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。