复杂字段类型的导入导出

复杂字段类型的导入导出


  1. 如果想要导出的字段是该模型关联的对象里的一个字段,则需要在创建模版时使用 “对象.字段” 的方式,并在导出时手动设置该字段

    例如PamirsEmployee模型中的company,使用company.name创建值

    @Field.many2one
    @Field.Relation(relationFields = {"companyCode"}, referenceFields = {"code"})
    @Field(displayName = "所属公司")
    private PamirsCompany company;
    //定义员工导入导出模版
    @Component
    public class EmployeeTemplate implements ExcelTemplateInit {
    
       public static final String TEMPLATE_NAME = "employeeTemplate";
    
       @Override
       public List generator() {
         //默认为导入导出模版,如需导入导出单独设置
         //导入:WorkbookDefinitionBuilder.newInstance(PamirsEmployee.MODEL_MODEL, TEMPLATE_NAME).setType(ExcelTemplateTypeEnum.IMPORT)
         //导出:WorkbookDefinitionBuilder builder = WorkbookDefinitionBuilder.newInstance(PamirsEmployee.MODEL_MODEL, TEMPLATE_NAME).setType(ExcelTemplateTypeEnum.EXPORT)
         //这里没有定义Type,导入导出都适用该模版
           WorkbookDefinitionBuilder builder = WorkbookDefinitionBuilder.newInstance(PamirsEmployee.MODEL_MODEL, TEMPLATE_NAME).setDisplayName("员工-导入导出");
    
           EmployeeTemplate.createEmployeeSheet(builder);
    
           return Collections.singletonList(builder.build());
       }
    
       private static void createEmployeeSheet(WorkbookDefinitionBuilder builder) {
           builder.setEachImport(true)
                   .createSheet().setName("员工")
                   .createBlock(PamirsEmployeeProxy.MODEL_MODEL, ExcelAnalysisTypeEnum.FIXED_HEADER, ExcelDirectionEnum.HORIZONTAL, "A1:K3")
                   .createMergeRange("A1:K1")
                   .createHeader().setStyleBuilder(ExcelHelper.createDefaultStyle()).setIsConfig(Boolean.TRUE)
                   .createCell().setField("code").setStyleBuilder(ExcelHelper.createDefaultStyle().setWidth(6000)).and()
                   .createCell().setField("name").setStyleBuilder(ExcelHelper.createDefaultStyle().setWidth(6000)).and()
                   //使用company.name获取PamirsCompany里面的name字段
                   .createCell().setField("company.name").setStyleBuilder(ExcelHelper.createDefaultStyle().setWidth(6000)).and()
                   ......
       }
    }
    
    //手动设置该字段,如2所示
    
  2. 如果想要导出的字段是非存储字段,由于默认只导出存储在数据库里的字段,非存储的字段需要在导出时手动设置

    @Slf4j
    @Component
    @Ext(ExcelExportTask.class)
    public class EmployeeTemplateExportExtPoint extends DefaultExcelExportFetchDataExtPoint {
    
        @Override
        @ExtPoint.Implement(expression = "context.name==\"" + EmployeeTemplate.TEMPLATE_NAME + "\"")
        public List<Object> fetchExportData(ExcelExportTask exportTask, ExcelDefinitionContext context) {
            return super.fetchExportData(exportTask, context);
        }
    
        //重写rawQueryList方法,使用listFieldQuery将非存储字段单独设置
        @Override
        protected List<?> rawQueryList(IWrapper<?> wrapper) {
            List<PamirsEmployee> pamirsEmployeeProxies = (List<PamirsEmployee>) Models.data().queryListByWrapper(wrapper);
            if (CollectionUtils.isNotEmpty(pamirsEmployeeProxies)) {
                new PamirsEmployee().listFieldQuery(pamirsEmployeeProxies, PamirsEmployee::getDepartmentList);
            }
         return pamirsEmployeeProxies;
     }
    }
  3. 如果想要导入的字段存在多个,则可以创建一个代理模型,在代理模型里设置一个字段用来接受该多值字段(在Excel里一个单元格内填写多值字段,每个字段用自定义符号(如:" ; ")进行分割),在创建模版时使用该代理类的模版,在导入导出的时候再根据 “;” 截取

    @Model.model(PamirsEmployeeProxy.MODEL_MODEL)
    @Model(displayName = "员工导出代理")
    @Model.Advanced(type = ModelTypeEnum.PROXY)
    public class PamirsEmployeeProxy extends PamirsEmployee {
    
        private static final long serialVersionUID = -6582160484690807999L;
    
        public static final String MODEL_MODEL = "business.PamirsEmployeeProxy";
    
        @Field.String
        @Field(displayName = "部门编码列表")
        private String departmentCodeList;
    }

    创建模版时创建代理模型的字段

    .createCell().setField("departmentCodeList")

    导入继承AbstractExcelImportDataExtPointImpl

    @Component
    @Ext(ExcelImportTask.class)
    @Slf4j
    public class EmployeeTemplateImportExtPoint extends AbstractExcelImportDataExtPointImpl<PamirsEmployeeProxy> {
        @Override
        @ExtPoint.Implement(expression = "importContext.definitionContext.name==\"" + EmployeeTemplate.TEMPLATE_NAME + "\"")
        public Boolean importData(ExcelImportContext importContext, PamirsEmployeeProxy data) {
            //TODO 根据逻辑校验数据
        String departmentCodeList = data.getDepartmentCodeList();
         if (StringUtils.isNotEmpty(departmentCodeList)) {
                String[] departmentCodes = departmentCodeList.split(";");
          ....
        }
          return true;
    }
    }

    导出继承DefaultExcelExportFetchDataExtPoint

    @Slf4j
    @Component
    @Ext(ExcelExportTask.class)
    @SuppressWarnings({"unchecked"})
    public class EmployeeTemplateExportExtPoint extends DefaultExcelExportFetchDataExtPoint {
    
     @Override
     @ExtPoint.Implement(expression = "context.name==\"" + EmployeeTemplate.TEMPLATE_NAME + "\"")
     public List<Object> fetchExportData(ExcelExportTask exportTask, ExcelDefinitionContext context) {
       //TODO 根据逻辑校验数据
            return super.fetchExportData(exportTask, context);
      }
    
     @Override
      protected List<?> rawQueryList(IWrapper<?> wrapper) {
        //TODO 根据逻辑校验行数据
       List<PamirsEmployeeProxy> pamirsEmployeeProxies = (List<PamirsEmployeeProxy>) Models.data().queryListByWrapper(wrapper);
        if (CollectionUtils.isNotEmpty(pamirsEmployeeProxies)) {
          for (PamirsEmployeeProxy pamirsEmployeeProxy : pamirsEmployeeProxies) {
           List<PamirsDepartment> departmentList = pamirsEmployeeProxy.getDepartmentList();
                  if (CollectionUtils.isNotEmpty(departmentList)) {
                            pamirsEmployeeProxy.setDepartmentCodeList(departmentList.stream()
                                                              .map(PamirsDepartment::getCode)
                                                              .collect(Collectors.joining(";")));
                }
         }
      }
      return pamirsEmployeeProxies;
    }
    }

Oinone社区 作者:mame原创文章,如若转载,请注明出处:https://doc.oinone.top/oinone-faq/14289.html

访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验

(0)
mame的头像mame数式员工
上一篇 2024年7月1日 下午1:58
下一篇 2024年7月2日 下午7:32

相关推荐

发表回复

登录后才能评论