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

   Template:
   
   <img width="1183" height="283" alt="Image" 
src="https://github.com/user-attachments/assets/efc435b0-1d2e-4302-a099-abe33f0f9f3b";
 />
   
   - Solution 1: Manual Formula Construction (Meets Formula Requirements)
   
   ```java
   class Issues172 {
   
       public static void main(String[] args) {
           FastExcel.write("output.xlsx")
                   .withTemplate("template.xlsx")
                   .registerWriteHandler(enableFormulaRecalculation())
                   .sheet()
                   .doFill(data(), 
FillConfig.builder().forceNewRow(true).build());
       }
   
       private static SheetWriteHandler enableFormulaRecalculation() {
           return new SheetWriteHandler() {
               @Override
               public void afterSheetCreate(SheetWriteHandlerContext context) {
                   
context.getWriteSheetHolder().getSheet().setForceFormulaRecalculation(true);
               }
           };
       }
   
       static List<Map<String, Object>> data() {
           return IntStream.range(1, 5)
                   .mapToObj(index -> {
                       Map<String, Object> map = new HashMap<>();
                       map.put("index", index);
                       map.put("vlimitName", "V-LIMIT_Name" + index);
                       map.put("nlimitUsageAmount", RandomUtils.nextInt(100, 
200));
                       map.put("nlimitUsageRatioYday", RandomUtils.nextInt(20, 
90));
                       map.put("change", buildFormulaCell(index));
                       map.put("vlimitPrompt", "V-LIMIT_Prompt");
   
                       return map;
                   })
                   .toList();
       }
   
       static WriteCellData<String> buildFormulaCell(Integer index) {
           // Start at row 7
           int currentRowIndex = index + 6;
           WriteCellData<String> result = new WriteCellData<>();
           FormulaData formulaData = new FormulaData();
           formulaData.setFormulaValue("SUM(D" + currentRowIndex + "-E" + 
currentRowIndex + ")");
           result.setFormulaData(formulaData);
           return result;
       }
   }
   ```
   
   Output:
   
   <img width="1264" height="461" alt="Image" 
src="https://github.com/user-attachments/assets/9d1d7217-64fa-4a76-a5fb-d9dd99362b40";
 />
   
   - Solution 2: Direct Calculation (Personally Recommended)  
   
   ```java
   class Issues172 {
   
       public static void main(String[] args) {
           FastExcel.write("output.xlsx")
                   .withTemplate("template.xlsx")
                   .sheet()
                   .doFill(data(), 
FillConfig.builder().forceNewRow(true).build());
   
           log.info("Done...");
       }
   
       static List<Map<String, Object>> data() {
           return IntStream.range(1, 5)
                   .mapToObj(index -> {
                       Map<String, Object> map = new HashMap<>();
                       map.put("index", index);
                       map.put("vlimitName", "V-LIMIT_Name" + index);
                       int one = RandomUtils.nextInt(100, 200);
                       map.put("nlimitUsageAmount", one);
                       int two = RandomUtils.nextInt(20, 90);
                       map.put("nlimitUsageRatioYday", two);
                       map.put("change", one - two);
                       map.put("vlimitPrompt", "V-LIMIT_Prompt");
   
                       return map;
                   })
                   .toList();
       }
   }
   ```
   
   Output:
   
   <img width="1264" height="467" alt="Image" 
src="https://github.com/user-attachments/assets/1850690a-de1a-48ca-ae8a-5e120b4cc463";
 />


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