Springboot批量上传

废话不说,直接贴代码,想用的直接把我查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());
     }
    

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值