复杂字段类型的导入导出
-
如果想要导出的字段是该模型关联的对象里的一个字段,则需要在创建模版时使用 “对象.字段” 的方式,并在导出时手动设置该字段
例如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<ExcelWorkbookDefinition> generator() { //可以返回多个模版,导出的时候页面上由用户选择导出模版 return Collections.singletonList( ExcelHelper.fixedHeader(PetShop.MODEL_MODEL, TEMPLATE_NAME) .createBlock(TEMPLATE_NAME, PetShop.MODEL_MODEL) .setType(ExcelTemplateTypeEnum.EXPORT) //使用company.name获取PamirsCompany里面的name字段 .addColumn("company.name", "所属公司") .build()); } } //手动设置该字段,如2所示
-
如果想要导出的字段是非存储字段,由于默认只导出存储在数据库里的字段,非存储的字段需要在导出时手动设置
@Slf4j @Component @Ext(ExcelExportTask.class) public class EmpTemplateExportExtPoint 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; } }
-
如果想要导入的字段存在多个,则可以创建一个代理模型,在代理模型里设置一个字段用来接受该多值字段(在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; }
创建模版时创建代理模型的字段
.addColumn("departmentCodeList", "部门编码列表")
导入:新建一个类,用来作为导入的扩展点,继承
AbstractExcelImportDataExtPointImpl
@Component @Ext(ExcelImportTask.class) @Slf4j public class EmpTemplateImportExtPoint extends AbstractExcelImportDataExtPointImpl<PamirsEmployeeProxy> { //必须加这个方法,它使用EmployeeTemplate.TEMPLATE_NAME来指定导入模版 @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 EmpTemplateExportExtPoint extends DefaultExcelExportFetchDataExtPoint { //必须加这个方法,它使用EmployeeTemplate.TEMPLATE_NAME来指定导出模版 @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)) { new PamirsEmployeeProxy().listFieldQuery(pamirsEmployeeProxies, PamirsEmployeeProxy::getDepartmentList); 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社区 作者:yexiu原创文章,如若转载,请注明出处:https://doc.oinone.top/oinone-faq/14289.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验