-
Notifications
You must be signed in to change notification settings - Fork 478
feature: Support Dynamic Column in Fill Operation for Collection Data #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
e57a2a7
1e2cdad
2fe3694
e8863d3
68dbedd
2fb9bc7
0245b39
ce222f2
9801ac9
e7fd883
4b4a62c
baabaab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package org.apache.fesod.excel.temp.fill; | ||
|
|
||
| import lombok.Data; | ||
| import org.apache.fesod.excel.annotation.fill.DynamicColumn; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Data | ||
| public class DynamicFillData { | ||
| private String name; | ||
| private double number; | ||
| @DynamicColumn() | ||
| private Map<String,String> qtyMap; | ||
|
|
||
| @DynamicColumn() | ||
| private Map<String,DynamicFillDataObj> priceMap; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.apache.fesod.excel.temp.fill; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @AllArgsConstructor | ||
| @Getter | ||
| @Setter | ||
| public class DynamicFillDataObj { | ||
|
|
||
| private String qty; | ||
| private double price; | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package org.apache.fesod.excel.annotation.fill; | ||
|
|
||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
| import java.lang.annotation.Inherited; | ||
|
|
||
| @Target(ElementType.FIELD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Inherited | ||
| public @interface DynamicColumn { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,14 +19,16 @@ | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| package org.apache.fesod.excel.write.executor; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||
| import cn.idev.excel.support.cglib.beans.BeanMap; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.commons.collections4.CollectionUtils; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.annotation.fill.DynamicColumn; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.context.WriteContext; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.converters.Converter; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.converters.ConverterKeyBuild; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.converters.NullableObjectConverter; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.converters.WriteConverterContext; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.enums.CellDataTypeEnum; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.enums.WriteDirectionEnum; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.exception.ExcelWriteDataConvertException; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.metadata.data.CommentData; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.metadata.data.FormulaData; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -35,13 +37,17 @@ | |||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.metadata.data.WriteCellData; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.metadata.property.ExcelContentProperty; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.support.ExcelTypeEnum; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.BeanMapUtils; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.DateUtils; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.FieldUtils; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.FileTypeUtils; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.ListUtils; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.StyleUtil; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.WorkBookUtil; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.util.WriteHandlerUtils; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.write.handler.context.CellWriteHandlerContext; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.write.metadata.fill.DynamicColumnInfo; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.fesod.excel.write.metadata.fill.FillConfig; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.ss.usermodel.Cell; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.ss.usermodel.ClientAnchor; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -53,6 +59,10 @@ | |||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.ss.usermodel.Workbook; | ||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import java.lang.reflect.Field; | ||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||
| * Excel write Executor | ||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -66,12 +76,11 @@ public AbstractExcelWriteExecutor(WriteContext writeContext) { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||
| * Transform the data and then to set into the cell | ||||||||||||||||||||||||||||||||||||||||||||
| * Transform item | ||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||
| * @param cellWriteHandlerContext context | ||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||
| protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| * */ | ||||||||||||||||||||||||||||||||||||||||||||
| protected void convertAndSetItem(CellWriteHandlerContext cellWriteHandlerContext) { | ||||||||||||||||||||||||||||||||||||||||||||
| WriteCellData<?> cellData = convert(cellWriteHandlerContext); | ||||||||||||||||||||||||||||||||||||||||||||
| cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(cellData)); | ||||||||||||||||||||||||||||||||||||||||||||
| cellWriteHandlerContext.setFirstCellData(cellData); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -98,6 +107,10 @@ protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) | |||||||||||||||||||||||||||||||||||||||||||
| cellData.setType(CellDataTypeEnum.EMPTY); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| Cell cell = cellWriteHandlerContext.getCell(); | ||||||||||||||||||||||||||||||||||||||||||||
| setCellValue(cellWriteHandlerContext, cellData, cell); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| private void setCellValue(CellWriteHandlerContext cellWriteHandlerContext, WriteCellData<?> cellData, Cell cell) { | ||||||||||||||||||||||||||||||||||||||||||||
| switch (cellData.getType()) { | ||||||||||||||||||||||||||||||||||||||||||||
| case STRING: | ||||||||||||||||||||||||||||||||||||||||||||
| cell.setCellValue(cellData.getStringValue()); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -126,6 +139,64 @@ protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||
| * Transform the data and then to set into the cell | ||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||
| * @param cellWriteHandlerContext context | ||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||
| protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) { | ||||||||||||||||||||||||||||||||||||||||||||
| Object originalValue = cellWriteHandlerContext.getOriginalValue(); | ||||||||||||||||||||||||||||||||||||||||||||
| Field field = cellWriteHandlerContext.getExcelContentProperty().getField(); | ||||||||||||||||||||||||||||||||||||||||||||
| if (null != field && field.isAnnotationPresent(DynamicColumn.class)) { | ||||||||||||||||||||||||||||||||||||||||||||
| Map<String, Object> dynamicColumnMap = (Map<String, Object>) originalValue; | ||||||||||||||||||||||||||||||||||||||||||||
| FillConfig fillConfig = cellWriteHandlerContext.getFillConfig(); | ||||||||||||||||||||||||||||||||||||||||||||
| if(null == fillConfig || null == fillConfig.getDynamicColumnInfoMap()){ | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| if(null == fillConfig || null == fillConfig.getDynamicColumnInfoMap()){ | |
| if(null == fillConfig || null == fillConfig.dynamicColumnInfoMap){ |
Outdated
Copilot
AI
Sep 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential IndexOutOfBoundsException if the originalVariable contains a dot but the split result doesn't have at least 2 elements. Should check array length before accessing index 1.
| key = originalVariable.split("\\.")[1]; | |
| Object itemBean = o; | |
| if (null == itemBean) { | |
| o = null; | |
| }else{ | |
| BeanMap beanMap = BeanMapUtils.create(itemBean); | |
| o = beanMap.get(key); | |
| String[] splitVar = originalVariable.split("\\."); | |
| if (splitVar.length >= 2) { | |
| key = splitVar[1]; | |
| Object itemBean = o; | |
| if (null == itemBean) { | |
| o = null; | |
| }else{ | |
| BeanMap beanMap = BeanMapUtils.create(itemBean); | |
| o = beanMap.get(key); | |
| } | |
| } else { | |
| // Optionally handle the case where there is no second part | |
| key = null; | |
| o = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raw type usage. Should use
new HashMap<>()ornew HashMap<String, Object>()for type safety.