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

   > The traversal-based sheet reading approach does not affect the triggering 
logic of `ReadListener#invoke()`, which still executes normally when parsing 
data rows.
   
   If this approach still doesn't meet your needs in your scenario, Consider 
using `ExcelAnalysisStopSheetException` combined with custom `hasNext` logic to 
make the parsing process only terminate the current sheet when the row limit is 
reached, instead of interrupting the entire workbook. Example code is as 
follows:
   
   <details>
   <summary>Code</summary>
   
   ```java
   @Slf4j
   class RunTest {
   
       static final String PATHNAME = "pathname...";
   
       public static void main(String[] args) {
   
           prepareData();
   
           PreDataListener listener = new PreDataListener();
   
           try (ExcelReader reader = 
FesodSheet.read(PATHNAME).head(ExcelModel.class).registerReadListener(listener).build())
 {
               List<ReadSheet> readSheets = 
reader.excelExecutor().sheetList().stream()
                       .peek(sheet -> sheet.setNumRows(5)).toList();
               for (ReadSheet readSheet : readSheets) {
                   reader.read(readSheet);
               }
               log.info("total count: {}", listener.datas.size());
           }
   
           log.info("done...");
       }
   
   
       @Slf4j
       public static class PreDataListener extends 
AnalysisEventListener<ExcelModel> {
   
           List<ExcelModel> datas = new ArrayList<>();
   
           @Override
           public void invoke(ExcelModel data, AnalysisContext context) {
               if (shouldStopCurrentSheet(context)) {
                   throw new ExcelAnalysisStopSheetException();
               }
   
               datas.add(data);
               log.info("data: {}", data);
           }
   
           @Override
           public void doAfterAllAnalysed(AnalysisContext context) {
               log.info("This method will not be executed");
           }
   
           @Override
           public boolean hasNext(AnalysisContext context) {
               return true;
           }
   
           private boolean shouldStopCurrentSheet(AnalysisContext context) {
               ReadRowHolder readRowHolder = context.readRowHolder();
               int index = readRowHolder.getRowIndex();
   
               Integer limit = 
context.readSheetHolder().getReadSheet().getNumRows();
               // If you need
               // if (limit == null) {
               //     limit = 
context.readWorkbookHolder().getReadWorkbook().getNumRows();
               // }
               return limit != null && index > limit;
           }
       }
   
       private static void prepareData() {
           try (ExcelWriter writer = 
FesodSheet.write(PATHNAME).head(ExcelModel.class).build()) {
   
               writer.write(data(0), 
FesodSheet.writerSheet().sheetNo(0).build());
               writer.write(data(1), 
FesodSheet.writerSheet().sheetNo(1).build());
           }
       }
   
       static List<ExcelModel> data(int sheetNo) {
           return IntStream.rangeClosed(1, 10)
                   .mapToObj(idx -> {
                       ExcelModel result = new ExcelModel();
                       result.setNo(idx);
                       result.setName("Sheet" + sheetNo + " - " + idx);
                       return result;
                   })
                   .toList();
       }
   
       @Data
       public static class ExcelModel {
           private Integer no;
           private String name;
       }
   }
   ```
   
   </details>


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