bengbengbalabalabeng commented on issue #656:
URL: https://github.com/apache/fesod/issues/656#issuecomment-4090322613

   > +1 不想用 index 如何解决
   
   Hi, @RoeGross 
   
   如果你不希望依赖 `index`,结合你提供 #886 表头模式也可以通过 `ReadListener` 自行合并多行表头。不过这种方式我个人不太推荐:
   
   - 灵活性有限
   - 后期维护成本对比 `index` 更高
   
   下面是一个简单示例:
   
   Model
   
   ```java
   @EqualsAndHashCode
   @Data
   public static class ExcelModel {
       @ExcelProperty(value = "表头")
       private String field1;
   
       @ExcelProperty(value = "表头2")
       private String field2;
       
       @ExcelProperty(value = "表头3")
       private String field3;
       
       @ExcelProperty(value = "表头4")
       private String field4;
       
       @ExcelProperty(value = "表头5")
       private String field5;
       
       @ExcelProperty(value = "表头6")
       private String field6;
       
       @ExcelProperty(value = "表头7")
       private String field7;
   }
   ```
   
   Listener
   
   ```java
   public static class ExcelHeaderMerger implements ReadListener<Object> {
   
       private Map<Integer, ReadCellData<?>> tmp;
   
       @Override
       public void invokeHead(Map<Integer, ReadCellData<?>> headMap, 
AnalysisContext context) {
           Integer rowIndex = context.readRowHolder().getRowIndex();
           if (rowIndex == 0) {
               tmp = new HashMap<>(headMap);
               tmp.remove(3);
           } else if (rowIndex == 1) {
               headMap.putAll(tmp);
               tmp = null;
           }
       }
   
       @Override
       public void invoke(Object o, AnalysisContext analysisContext) {
           // do nothing
       }
   
       @Override
       public void doAfterAllAnalysed(AnalysisContext analysisContext) {
           // do nothing
       }
   }
   ```
   
   Demo
   
   ```java
   List<ExcelModel> rows = FesodSheet.read(pathname)
                   .head(ExcelModel.class)
                   .registerReadListener(new ExcelHeaderMerger())
                   .headRowNumber(2)
                   .sheet()
                   .doReadSync();
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to