java用实现excel创建模板和下载模板
@Transactional(rollbackFor = Exception.class)
@PostMapping("upload")
public ActionResult<Boolean> upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
try {
Long userId = (Long) request.getAttribute(LoginParamsEnum.USERID.getName());
if(null == userId || 0 == userId){
return ActionResult.getErrorResult(InfoCodeEnum.NOT_LOGIN_CODE.getCode(),"用户未登录");
}
String originalFilename = file.getOriginalFilename();
UploadFileInfoDO uploadFileInfoDO = new UploadFileInfoDO();
uploadFileInfoDO.setFileName(originalFilename);
uploadFileInfoDO.setFileSize(file.getSize());
String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
//判断上传的文件格式
if (originalFilename == null || !".xlsx".equals(fileType)) {
log.error("file format is error !");
return ActionResult.getErrorResult("文件格式错误,请修改重新导入");
}
String fileName = originalFilename.substring(0, originalFilename.lastIndexOf("."));
String filePath = tempFilePath + "/" + fileName + "_" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + fileType;
File tempFile = new File(filePath);
if (!tempFile.exists()) {
tempFile.createNewFile();
}
uploadFileInfoDO.setFilePath(filePath);
file.transferTo(tempFile);
String fileMD5 = FileMD5Util.getFileMD5(tempFile);
Boolean isExist = fileService.isExistFileByFileMd5(fileMD5);
if (isExist) {
log.error("上传重复文件!");
return ActionResult.getErrorResult("请不要上传重复文件!");
}
uploadFileInfoDO.setFileMd5(fileMD5);
uploadFileInfoDO.setStatus(1L);
Boolean saveFile = fileService.saveFile(uploadFileInfoDO);
if (saveFile) {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(servletRequestAttributes,true);
scheduledExecutorService.schedule(() -> {
log.info("解析文件开始...");
dealBatchImport(uploadFileInfoDO,userId);
log.info("解析文件结束...");
}, 3, TimeUnit.SECONDS);
}
return ActionResult.getSuccessResult(true);
} catch (Exception e) {
log.error("用户数据导入异常: ", e);
return ActionResult.getErrorResult("用户数据导入异常");
}
}
/**
* 批量导入
*
* @param uploadFileInfoDO
*/
private void dealBatchImport(UploadFileInfoDO uploadFileInfoDO,Long userId) {
try {
ExcelReader reader = ExcelUtil.getReader(uploadFileInfoDO.getFilePath());
List<Map<String, Object>> maps = reader.readAll();
int num = maps.size();
uploadFileInfoDO.setAllNum(Long.parseLong(num + ""));
int errNum = 0;
List<VoicesTestDO> voicesTestDOS = new ArrayList<>();
for (int i = 0; i < num; i++) {
Map<String, Object> map = maps.get(i);
Collection<Object> values = map.values();
Object[] objects = values.toArray();
VoiceTestTemplateVO vo = new VoiceTestTemplateVO();
vo.setVoiceid(objects[0].toString());
、、、、、、
voicesTestDO.setCreateBy(userId+"");
voicesTestDOS.add(voicesTestDO);
}
if(CollUtil.isNotEmpty(voicesTestDOS)) {
voicesService.batchInsertVoiceTest(voicesTestDOS);
}
// 删除文件
File file = new File(uploadFileInfoDO.getFilePath());
file.deleteOnExit();
// 更改上传文件的状态
uploadFileInfoDO.setStatus(2L);
fileService.updateFileInfo(uploadFileInfoDO);
} catch (Exception e) {
log.error("文件解析异常:", e);
uploadFileInfoDO.setStatus(3L);
fileService.updateFileInfo(uploadFileInfoDO);
}
}