废话不说,直接贴代码,想用的直接把我查sql换成你的就可以用
Controller
@PostMapping("/batchAddUDoctor")
@ApiOperation(“批量录入医生信息”)
public ApiResponse batchAddUser (@CurrentUser LoginUser loginUser, @RequestParam(value = “file”,required = true) MultipartFile file,
@RequestParam(value = “page”,required = true) Integer page,
@RequestParam(value = “size”,required = true) Integer sizes) throws Exception {
BackendUserVO user = (BackendUserVO) loginUser;
log.debug(“开始上传Excel文件”);
try {
if (file == null) {
ApiResponse.fail(BachGroundError.CANSHU_NULL);
}
String fileName = file.getOriginalFilename();
if (!fileName.matches("^.+\.(?i)(xls)KaTeX parse error: Expected 'EOF', got '&' at position 4: ") &̲& !fileName.mat…")) {
return ApiResponse.fail(BachGroundError.EXCIL_NOT_AGREE);
}
long size = file.getSize();
if (StringUtils.isEmpty(fileName) || size == 0) {
// 文件不能为空
return ApiResponse.fail(BachGroundError.EXCIL_IS_NULLS);
}
return uploadExcel.uploadExcel(user,fileName, file,page,sizes);
} catch (ServiceException e) {
e.printStackTrace();
}
return ApiResponse.success();
}
/**
-
@version 1.0 这个直接拿走,少的应该就是sql语句了,直接换了就可以
-
@date 2021/9/7 10:15
*/
@Service
@Slf4j
public class UploadExcelFileService {@Autowired
private EapUserTMapper eapUserTMapper;
@Autowired
private EapQuestionModuleTMapper eapQuestionModuleTMapper;@Autowired
private EapQuestionTestcenterTMapper eapQuestionTestcenterTMapper;
@Autowired
private EapUserTestItemsTMapper eapUserTestItemsTMapper;public ApiResponse uploadExcel(BackendUserVO user, String fileName, MultipartFile file,Integer number,Integer size) throws ServiceException {
try {
PrintAddUserVO printAddUserVO=new PrintAddUserVO();
log.debug(“开始处理Excel文件!”);
InputStream inputStream = file.getInputStream();
Workbook wb;
if (fileName.matches("^.+\.(?i)(xlsx)$")) {
wb = new XSSFWorkbook(inputStream);
} else {
wb = new HSSFWorkbook(inputStream);
}
Sheet sheet = wb.getSheetAt(0);
if (sheet == null) {
ApiResponse.fail(BachGroundError.EXCIL_IS_NULLSss);
}// 列数 int column = sheet.getRow(0).getPhysicalNumberOfCells(); log.debug("Excel列数:" + column); // 此处可判断Excel列数是否符合要求 // 行数 int rows = sheet.getLastRowNum(); log.debug("Excel行数:" + rows); // 此处可判断Excel行数是否符合要求 ArrayList<MMPIInfoVO> objects1 = new ArrayList<>(); //查配置的报告可见方式 String visiable = eapQuestionModuleTMapper.getVisiable(user.getTestCenterId().toString()); // 循环Excel for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) { continue; } MMPIInfoVO excelData = new MMPIInfoVO(); // 第一行 if (row.getCell(0) != null) { row.getCell(0).setCellType(CellType.STRING); //excil里的的第一列姓名 XSSFCell cell = (XSSFCell) row.getCell(0); cell.setCellType(CellType.STRING); String name = cell.getStringCellValue(); //excil里的的第二列手机号 XSSFCell cells = (XSSFCell) row.getCell(1); cells.setCellType(CellType.STRING); String phone = cells.getStringCellValue(); excelData.setName(name); excelData.setPhone(phone); EapUserT oneByPhone = eapUserTMapper.getOneByPhone(phone, user.getTestCenterId().toString()); if(Objects.isNull(oneByPhone)){ EapUserT eapUserT=new EapUserT(); eapUserT.setName(name); eapUserT.setPhone(phone); eapUserT.setTestcenterId(user.getTestCenterId()); eapUserT.setRemoved(EnumYesOrNo.N.getValue()); eapUserT.setCreateAt(LocalDateTime.now()); eapUserT.setUserType(EnumUserType.DOCTOR.getValue()); eapUserT.setVisible(visiable); eapUserT.setPassword("123456"); eapUserTMapper.insert(eapUserT); insertUserItems(eapUserT,EnumUserType.DOCTOR.getValue()); }else{ excelData.setRemark("错误,手机号重复,请重新上传"); objects1.add(excelData); } } } int total = objects1.size(); List list = startPage(objects1, number, size); Integer pageCount = 0; // 页数 if (total % size == 0) { pageCount = total / size; } else { pageCount = total / size + 1; } printAddUserVO.setRecords(list); printAddUserVO.setCurrent_page(number); printAddUserVO.setMax_page(pageCount); printAddUserVO.setTotal_count(total); printAddUserVO.setPage_size(size); return ApiResponse.success(printAddUserVO); } catch (IOException e) { e.printStackTrace(); log.error(">>>>>>>>>>>>>保存Excel数据到数据库【UploadExcelFileServiceImpl.uploadExcel()】执行异常:" + e); throw new ServiceException(e.getMessage()); }
}
}