I have a requirement where I need to get values based on some excel formula fields. On a high level, we have an excel where user has the liberty to enter date values and based on these input the formula fields are updated and total revenue field value is calculated. Now to perform these validations via code will it be better to
- Write own utilities in Java for each formula in spreadsheet.
- Use Apache POI to input the values in spreadsheet on runtime and evaluate formulas to get revenue value.
On approach 1 I am having below issues :
On implementing below code for =YEARFRAC(B1, B2+1)*12
formula
public static double calculateContractLength(String startdate,String enddate) {
double monthsBetween = ChronoUnit.MONTHS.between(
LocalDate.parse(startdate),
LocalDate.parse(enddate));
return monthsBetween;
}
I see mismatch in result of excel formula and above code. For Instance Start Date = 2/12/2020 End Date = 12/31/2022, I see excel showing 34.63 but Code returning 34.0
To better handle is I want to know if there are some existing libraries which have already implemented these spreadsheet formulas or just updating the excel at run time with Apache POI would be a better solution.