hooj0 opened a new pull request, #619:
URL: https://github.com/apache/fesod/pull/619

   <!--
   Thanks very much for contributing to Apache Fesod (Incubating)! Please make 
sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   Closed: #ISSUE
   Related: #ISSUE
   
   -->
   
   ## Purpose of the pull request
   
   <!-- Describe the purpose of this pull request. For example: Closed: 
#ISSUE-->
   Supports obtaining current row data object during writing #598 
   https://github.com/apache/fesod/issues/598
   
   
   ## What's changed?
   
   <!--- Describe the change below, including rationale and design decisions -->
   ### 增加上下文属性
   - 在 `WriteConverterContext` 中可以获取到原始数据对象,通过 `getRecord `方法
   - 在 `CellWriteHandlerContext` 中可以获取到原始数据对象,通过 `getOriginalRecord`方法
   
   ### 设置当前数据对象到上下文属性中
   - 在 `AbstractExcelWriteExecutor#doConvert` 方法中设置当前对象值
   ```java
   cellData = ((Converter<Object>) converter)
                       .convertToExcelData(new WriteConverterContext<>(
                               cellWriteHandlerContext.getOriginalValue(), 
cellWriteHandlerContext.getOriginalRecord(), excelContentProperty, 
writeContext));
   ```
   - 在 
`ExcelWriteAddExecutor#doAddBasicTypeToExcel/addJavaObjectToExcel/addBasicTypeToExcel`
 方法中设置当前对象值 `setOriginalRecord`
   ```java
   cellWriteHandlerContext.setOriginalRecord(oneRowData);
   cellWriteHandlerContext.setOriginalValue(oneRowData.get(dataIndex));
   cellWriteHandlerContext.setOriginalFieldClass(
           
FieldUtils.getFieldClass(cellWriteHandlerContext.getOriginalValue()));
   converterAndSet(cellWriteHandlerContext);
   
   WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
   ```
   - 在 `ExcelWriteFillExecutor#doFill` 方法中设置当前对象值 `setOriginalRecord`
   ```java
   createCell(analysisCell, fillConfig, cellWriteHandlerContext, 
rowWriteHandlerContext);
   cellWriteHandlerContext.setOriginalRecord(oneRowData);
   cellWriteHandlerContext.setOriginalValue(value);
   
cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, 
variable, value));
   
   converterAndSet(cellWriteHandlerContext);
   WriteCellData<?> cellData = cellWriteHandlerContext.getFirstCellData();
   ```
   
   ### 增加测试用例
   以下是测试用例需要的对象,`WriteConverter`和`CellWriteHandler`
   ```java
   @Getter
   @Setter
   @ToString
   @EqualsAndHashCode
   public class RecordData {
       /**
        * String Title
        */
       @ExcelProperty("String Title")
       private String string;
   
       /**
        * Date Title
        */
       @ExcelProperty("Date Title")
       private Date date;
   
       /**
        * Number Title
        */
       @ExcelProperty("Number Title")
       private Double doubleData;
   
       /**
        * 通过自定义转换器转换
        */
       @ExcelProperty(converter = RecordStringStringConverter.class)
       private String text;
   }
   
   // 可以访问到原始数据,进行其他业务处理
   @Slf4j
   public class RecordStringStringConverter implements Converter<String> {
       @Override
       public Class<?> supportJavaTypeKey() {
           return String.class;
       }
   
       @Override
       public CellDataTypeEnum supportExcelTypeKey() {
           return CellDataTypeEnum.STRING;
       }
   
       @Override
       public String convertToJavaData(ReadConverterContext<?> context) {
           return context.getReadCellData().getStringValue();
       }
   
       /**
        * 这里是写的时候会可以访问到原始数据,你可以进行其他业务处理,然后进行转换
        */
       @Override
       public WriteCellData<?> convertToExcelData(WriteConverterContext<String> 
context) {
           // 获取原始数据
           RecordData record = (RecordData) context.getRecord();
           log.info("原始数据:{}", record);
           return new WriteCellData<>("自定义:" + context.getValue() + "-" + 
record.getDoubleData());
       }
   }
   
   
   /**
    * 拦截器中单元格上下文可以获取到行数据
    */
   @Slf4j
   public class RecordCellWriteHandler implements CellWriteHandler {
   
       @Override
       public void afterCellDispose(CellWriteHandlerContext context) {
           Cell cell = context.getCell();
   
           if (BooleanUtils.isFalse(context.getHead()) && cell.getColumnIndex() 
!= 0) {
               RecordData record = (RecordData) context.getOriginalRecord();
               log.info("写入数据:{}", record);
           }
       }
   }
   ```
   
   测试用例代码
   ```java
   @Test
   public void recordWrite() {
       String fileName = TestFileUtil.getPath() + "recordWrite" + 
System.currentTimeMillis() + ".xlsx";
       FastExcel.write(fileName, RecordData.class)
               .registerWriteHandler(new RecordCellWriteHandler())
               .sheet("模板")
               .doWrite(records());
   }
   ```
   
   ### 执行结果
   <img width="1705" height="470" alt="image" 
src="https://github.com/user-attachments/assets/b4e90cc5-c897-43c7-8dc7-c7d71a5b8227";
 />
   <img width="505" height="384" alt="image" 
src="https://github.com/user-attachments/assets/a0ea7117-5dcd-4bcf-b636-2e4738abf7f2";
 />
   
   
   ## Checklist
   
   - [x] I have read the [Contributor 
Guide](https://fesod.apache.org/community/contribution).
   - [x] I have written the necessary doc or comment.
   - [x] I have added the necessary unit tests and all cases have passed.


-- 
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