Java 导入导出Excel


导入导出Excel

@Override
@Transactional(rollbackFor = Exception.class)
public Result<?> importBatchMaintainAssetExcel(HttpServletRequest request) throws IOException {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
    if(loginUser==null){throw new JeecgBootException("登录失效,请重新登录");}
    String username = loginUser.getUsername();
    Date date = new Date();
    // 错误信息
    List<String> errorMessage = new ArrayList<>();
    int successLines = 0, errorLines = 0;
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        MultipartFile file = entity.getValue();// 获取上传文件对象
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setTitleRows(2);
        params.setNeedSave(true);
        try {
            List<AssetMaintainExcelImportDTO>  importAssetScraps = ExcelImportUtil.importExcel(file.getInputStream(), AssetMaintainExcelImportDTO.class, params);
            List<AssetMaintainDTO>  list = new ArrayList<>();
            int i=2;
            //先校验参数
            if(CollectionUtils.isEmpty(importAssetScraps)){throw new JeecgBootException("维修信息为空");}
            for (AssetMaintainExcelImportDTO importAssetScrap : importAssetScraps) {
                try {
                    i = i + 1;
                    String assetNo = importAssetScrap.getAssetNo();
                    String remark = importAssetScrap.getRemark();

                    if(StringUtils.isBlank(assetNo)){throw new JeecgBootException("第"+i+"行 资产编号为空");}

                    AssetInfo assetInfo = iAssetInfoService.getByAssetNo(assetNo);
                    if(assetInfo==null){throw new JeecgBootException("第"+i+"行 资产编号错误【"+assetNo+"】");}
                    if(!AssetStatusEnum.INSTORE.getValue().equals(assetInfo.getAssetStatus())){throw new JeecgBootException("第"+i+"行 资产【"+assetNo+"】不是库存状态");}
                    assetInfo.setAssetStatus(AssetStatusEnum.MAINTENANCE.getValue());
                    AssetInfoDTO assetInfoDTO = new AssetInfoDTO();
                    assetInfoDTO.setAssetInfo(assetInfo);
                    iAssetInfoService.assetInfoCheck(assetInfoDTO);
                    AssetMaintainDTO assetScrapDTO = new AssetMaintainDTO();
                    assetScrapDTO.setAssetInfoDTO(assetInfoDTO);
                    assetScrapDTO.setCreateTime(date);
                    assetScrapDTO.setRemark(remark);
                    assetScrapDTO.setCreateBy(username);

                    list.add(assetScrapDTO);
                }catch (JeecgBootException je){
                    throw new JeecgBootException("第"+i+"行数据错误,"+je.getMessage());
                }
            }
            //没有任何问题,再开始保存
            for (AssetMaintainDTO assetScrapDTO : list) {
                try {
                    this.saveMaintainAssetInfo(assetScrapDTO);
                } catch (JeecgBootException je) {
                    errorLines += 1;
                    errorMessage.add(je.getMessage());
                }catch (Exception e) {
                    errorLines += 1;
                    errorMessage.add(e.getMessage());
                }
            }
            successLines+=(importAssetScraps.size()-errorLines);
        }catch (JeecgBootException je){
            return Result.error("文件导入失败:" + je.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("文件导入失败:" + e.getMessage());
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return ImportExcelUtil.imporReturnRes(errorLines,successLines,errorMessage);
}
@AutoLog(value = "资产合并-导出")
@ApiOperation(value = "资产合并-导出", notes = "资产合并-导出")
@RequestMapping(value = "/exportAssetMergeXls")
public ModelAndView exportAssetMergeXls(AssetMergeReq req, HttpServletRequest request) {
    ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
    List<AssetMergeVO> assetMergeVOList = iAssetMergeService.getAssetMergeList(req);
    //导出文件名称
    mv.addObject(NormalExcelConstants.FILE_NAME, "资产合并列表");
    mv.addObject(NormalExcelConstants.CLASS, AssetMergeVO.class);
    LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
    mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("资产合并列表", "导出人:" + user.getRealname(), "导出信息"));
    mv.addObject(NormalExcelConstants.DATA_LIST, assetMergeVOList);
    return mv;
}


0 0
讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
帮助