nyc1215 opened a new issue, #799:
URL: https://github.com/apache/fesod/issues/799

   ### Search before asking
   
   - [x] I searched in the [issues](https://github.com/apache/fesod/issues) and 
found nothing similar.
   
   
   ### Fesod version
   
   2.0.0-incubating-rc2
   
   ### JDK version
   
   1.8.0_202
   
   ### Operating system
   
   Windows 11 25H2
   
   ### Steps To Reproduce
   
   ```Java
   import lombok.AllArgsConstructor;
   import lombok.Data;
   import org.apache.fesod.sheet.ExcelWriter;
   import org.apache.fesod.sheet.FesodSheet;
   import org.apache.fesod.sheet.annotation.ExcelProperty;
   import org.apache.fesod.sheet.write.handler.SheetWriteHandler;
   import org.apache.fesod.sheet.write.handler.context.SheetWriteHandlerContext;
   import org.apache.fesod.sheet.write.metadata.WriteSheet;
   import org.apache.fesod.sheet.write.metadata.WriteTable;
   import org.apache.fesod.sheet.write.metadata.holder.WriteSheetHolder;
   import org.apache.poi.ss.usermodel.Cell;
   import org.apache.poi.ss.usermodel.Row;
   import org.apache.poi.ss.usermodel.Sheet;
   import org.junit.Test;
   
   import java.util.ArrayList;
   import java.util.Date;
   import java.util.List;
   
   public class AfterSheetDisposeIssueTest {
       @Data
       @AllArgsConstructor
       public static class DemoData {
           @ExcelProperty("字符串")
           private String str;
           @ExcelProperty("数字")
           private int number;
           @ExcelProperty("日期")
           private Date date;
       }
   
       public static class TestSheetWriteHandler implements SheetWriteHandler {
           @Override
           public void afterSheetDispose(SheetWriteHandlerContext context) {
               WriteSheetHolder writeSheetHolder = 
context.getWriteSheetHolder();
               Sheet sheet = writeSheetHolder.getSheet();
               int lastRowNum = sheet.getLastRowNum();
   
               //create a new row
               Row statisticRow = sheet.createRow(lastRowNum);
               Cell cell = statisticRow.createCell(0);
               cell.setCellValue("new cell in afterSheetDispose");
           }
       }
   
       @Test
       public void tableWrite() {
           String fileName = "tableWrite" + System.currentTimeMillis() + 
".xlsx";
   
           List<DemoData> data1 = new ArrayList<>();
           data1.add(new DemoData("字符串0", 1, new Date()));
           data1.add(new DemoData("字符串1", 2, new Date()));
           data1.add(new DemoData("字符串2", 3, new Date()));
           data1.add(new DemoData("字符串3", 4, new Date()));
           data1.add(new DemoData("字符串4", 5, new Date()));
   
           List<DemoData> data2 = new ArrayList<>();
           data2.add(new DemoData("字符串5", 1, new Date()));
           data2.add(new DemoData("字符串6", 2, new Date()));
           data2.add(new DemoData("字符串7", 3, new Date()));
           data2.add(new DemoData("字符串8", 4, new Date()));
           data2.add(new DemoData("字符串9", 5, new Date()));
   
           try (ExcelWriter excelWriter = FesodSheet.write(fileName, 
DemoData.class).build()) {
               WriteSheet writeSheet = FesodSheet.writerSheet("Table示例")
                       .needHead(Boolean.FALSE)
                       .registerWriteHandler(new TestSheetWriteHandler())
                       .build();
               WriteTable table1 = 
FesodSheet.writerTable(0).needHead(Boolean.TRUE).build();
               WriteTable table2 = 
FesodSheet.writerTable(1).needHead(Boolean.TRUE).build();
   
               //afterSheetDispose will be executed twice
               excelWriter.write(data1, writeSheet, table1);
               excelWriter.write(data2, writeSheet, table2);
           }
       }
   }
   ```
   
   ### Current Behavior
   
   Cells created in the `SheetWriteHandler.afterSheetDispose` method (with the 
content "new cell in afterSheetDispose") appear twice :
   
   <img width="408" height="375" alt="Image" 
src="https://github.com/user-attachments/assets/78248318-c434-464a-b417-4e602f9ef0e3";
 />
   
   Because each call to the `ExcelWriter.write` method internally invokes 
`WriteHandlerUtils.afterSheetDispose(this.context)`.
   
   ```Java
   public class ExcelBuilderImpl implements ExcelBuilder {
       .....
       public void addContent(Collection <? > data, WriteSheet writeSheet, 
WriteTable writeTable) {
           try {
               this.context.currentSheet(writeSheet, WriteTypeEnum.ADD);
               this.context.currentTable(writeTable);
               if(this.excelWriteAddExecutor == null) {
                   this.excelWriteAddExecutor = new 
ExcelWriteAddExecutor(this.context);
               }
               this.excelWriteAddExecutor.add(data);
               WriteHandlerUtils.afterSheetDispose(this.context); //execute
           } catch(RuntimeException e) {
               this.finishOnException();
               throw e;
           } catch(Throwable e) {
               this.finishOnException();
               throw new ExcelGenerateException(e);
           }
       }
       .....
   }
   ```
   
   ### Expected Behavior
   
   `SheetWriteHandler.afterSheetDispose` to be executed only once per sheet 
after all tables have been written.
   
   ### Anything else?
   
   I'm not sure if this behavior is intentional, or whether adding new rows in 
afterSheetDispose is simply not the correct usage.
   If this is considered a bug, will the fix be included in the 2.0.0 release?
   
   ### Are you willing to submit a PR?
   
   - [ ] I'm willing to submit a PR!


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