easy Excel

Posted 无敌的星哥哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easy Excel相关的知识,希望对你有一定的参考价值。

导出实体类

@Data
@ColumnWidth(20)//表头宽度
@HeadStyle(horizontalAlignment = HorizontalAlignment.CENTER)//表头样式
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER)//内容样式
@Service(value = "ReasonInfoExport")
public class InfoExport extends SuperModel

 
  @ApiModelProperty(value = "名称")
  @ExcelProperty("名称")
  private String Name;

controll

@ApiOperation(value = "导出",notes = "export data")
    @GetMapping("/export")
    public void exportAllEntity(EnforceReasonInfoParam param,
                                @RequestParam(value = "cols", required = false) List<String> cols,
                                @RequestParam(value = "ids", required = false) List<String> ids,
                                HttpServletResponse response) 
        QueryWrapper<EnforceReasonInfo> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(param)) 
            queryWrapper = createQuery(param);
        
        if (Objects.nonNull(ids) && !ids.isEmpty()) 
            queryWrapper.in(GridCaseHisParam.ID, ids);
        
        List<EnforceReasonInfo> resultList = enforceReasonInfoService.list(queryWrapper);

        List<EnforceReasonInfoExport> exports = new ArrayList<>();
        for (EnforceReasonInfo record : resultList) 
            EnforceReasonInfoExport enforceReasonInfoExport = new EnforceReasonInfoExport();
         
            if (StringUtils.isNotEmpty(record.getPlotRatioFlag())) 
                String dictNameByKey = sysDictDataService.getDictNameByKey(DictConstants.YES_NO,record.getPlotRatioFlag());
                enforceReasonInfoExport.setPlotRatioFlag(dictNameByKey);
            
            if (record.getNumFlag()!= null) 
                String dictNameByKey = sysDictDataService.getDictNameByKey(DictConstants.YES_NO,record.getNumFlag().toString());
                enforceReasonInfoExport.setNumFlag(dictNameByKey);
            
            if (StringUtils.isNotEmpty(record.getCaseReasonChooseFlag())) 
                String dictNameByKey = sysDictDataService.getDictNameByKey(DictConstants.YES_NO,record.getCaseReasonChooseFlag());
                enforceReasonInfoExport.setCaseReasonChooseFlag(dictNameByKey);
            
            exports.add(enforceReasonInfoExport);
        
        try 
            this.excelService.export("明细-EXCEL", exports, EnforceReasonInfoExport.class, response, cols);
         catch (IOException var11) 
            log.error("Export Error ", var11.fillInStackTrace());
            throw new ApiException(ExcelEnum.EXPORT_ERROR.transform());
        
    

导入实体类 及response

@Data
public class ReasonInfoImport 

    /**
     * 名称
     */
    @ApiModelProperty(value = "名称")
    @ExcelProperty("名称")
    @NotNull(message = "名称不能为空")
    private String Name;
//response
@Data
public class ReasonInfoResponse implements java.io.Serializable 
  private static final long serialVersionUID = 6826701812573993439L;
  private int successTotal;
  private int errorTotal;
  private List<ColsDto> beans;
  private List<ImportError<InfoImport>> errors;
  private String fileName;


controller
@ApiOperation(value="案由类型 导入Excel",notes = "根据Excel导入数据")
    @PostMapping("/upload")
    public BaseResponse<ReasonInfoResponse> exportShift(@RequestParam MultipartFile file)
        BaseResponse<ReasonInfoResponse> baseResponse = new BaseResponse<>();
        ReasonInfoResponse reasonInfoResponse = new ReasonInfoResponse();
        if (file.isEmpty()) 
            throw new ApiException(ExcelEnum.NO_EXPORT_FILE.transform());
        
        String filePath = excelService.importExcel(file);
        TongExcelListener<ReasonInfoImport> excelListener =
                new TongExcelListener(reasonInfoImportService,ReasonInfoImport.class);
        EasyExcel.read(filePath,ReasonInfoImport.class,excelListener).sheet().doRead();
        List<ImportError<ReasonInfoImport>> errorList = excelListener.getErrorList();
        reasonInfoResponse.setErrors(errorList.size() > 19 ? errorList.subList(0, 19) : errorList);
        reasonInfoResponse.setErrorTotal(errorList.size());
        reasonInfoResponse.setSuccessTotal(excelListener.getSuccessList().size());
        reasonInfoResponse.setBeans(ExcelDtoHelper.getCols(new ReasonInfoImport()));
        baseResponse.setData(reasonInfoResponse);
        return baseResponse;
    

导入service

public ImportCheckResult<ReasonInfoImport> doImportData(
            Map<String, ReasonInfoImport> insertMap, boolean insertTrigger) 
        List<ReasonInfoImport> successData = new ArrayList<>();
        List<ImportError<ReasonInfoImport>> errorData = new ArrayList<>();
        List<EnforceReasonInfo> insertData = new ArrayList<>();
        for (String dataId : insertMap.keySet()) 
            ReasonInfoImport reasonInfoImport = insertMap.get(dataId);
            StringBuilder checkResult = checkImportData(reasonInfoImport);
            if (StringUtils.isNotEmpty(checkResult)) 
                ImportError importError =
                        ImportError.builder()
                                .errorMsg(checkResult.toString())
                                .lineNumber(Integer.parseInt(dataId))
                                .data(reasonInfoImport)
                                .build();
                errorData.add(importError);
             else 
                EnforceReasonInfo converter = this.converter(reasonInfoImport);
                if (StringUtils.isNotEmpty(result)) 
                    ImportError importError = ImportError.builder().errorMsg(result.toString()).lineNumber(Integer.parseInt(dataId)).data(reasonInfoImport).build();
                    errorData.add(importError);
                 else 
                    successData.add(reasonInfoImport);
                    insertData.add(converter);
                
            
        
        if (!insertData.isEmpty() && insertTrigger && result.toString().equals("")) 
            reasonInfoService.saveBatch(insertData);
        
        return new ImportCheckResult<>(successData, errorData);
    

    private EnforceReasonInfo converter(ReasonInfoImport reasonInfoImport) 
        EnforceReasonInfo enforceReasonInfo = BeanHelper.beanToBean(reasonInfoImport, EnforceReasonInfo.class);
        String userId =
                Optional.ofNullable((String) RequestUtil.getRequest().getAttribute(ApiConstants.CLAIM_KEY))
                        .orElse("test");
        SysUser user = sysUserService.getById(userId);
        String orgid = user.getOrgId();
        SysOrg org = sysOrgService.getById(orgid);
        enforceReasonInfo.setCreateBy(userId);
        enforceReasonInfo.setUpdateBy(userId);
        enforceReasonInfo.setCreateDate(LocalDateTime.now());
        enforceReasonInfo.setUpdateDate(LocalDateTime.now());
        enforceReasonInfo.setStatus(BizConstants.STATUS_ENABLE);
        enforceReasonInfo.setCaseReasonCode(enforceReasonInfo.getCaseReasonCode());
        enforceReasonInfo.setCaseReasonName(enforceReasonInfo.getCaseReasonName());
        QueryWrapper<EnforceReasonType> wrapper = new QueryWrapper<>();
        wrapper.eq(EnforceReasonType.CASE_REASON_TYPE_NAME, enforceReasonInfo.getCaseReasonTypeId());
        EnforceReasonType one = reasonTypeService.getOne(wrapper);
        if (Objects.nonNull(one)) 
            enforceReasonInfo.setCaseReasonTypeId(one.getId());
            enforceReasonInfo.setAllParentIds("0," + one.getId());
         else 
            result.append("信息不存在;");
        

      
        return enforceReasonInfo;
    

    private StringBuilder checkImportData(ReasonInfoImport data) 
        result = result.delete(0, result.length());
        return result;
    

以上是关于easy Excel的主要内容,如果未能解决你的问题,请参考以下文章

easy-excel复杂格式

easy excel

easy Excel

PHP 从PHP导出Easy Excel

easy Excel

easy Excel