Apache poi cannot interpret VBA but of course it has a formula evaluator. So of course you can evaluate Excel formulas directly in apache poi code.
But the VBA function MonthName is not implemented by default. So either you do getting the date from the cell, what is clearly possible using apache poi. And then, you get the month and the month name out of that date using Java code. This is called first approach in following example.
Or you are using a implemented Excel function. TEXT for example. This is called second approach in following example.
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.*;
public class ExcelEvaluateMonthFunctions{
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("workbook.xlsx"));
Sheet sheet = workbook.getSheet("Sheet2");
java.util.Locale.setDefault(java.util.Locale.US);
//first approach:
String monthname = null;
java.util.Date date = sheet.getRow(11).getCell(2).getDateCellValue(); //Sheet2!C12 must contain a date
java.time.LocalDate localDate = date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
monthname = localDate.getMonth().getDisplayName(java.time.format.TextStyle.FULL, java.util.Locale.getDefault());
System.out.println(monthname);
//second approach:
monthname = null;
CreationHelper helper = workbook.getCreationHelper();
XSSFFormulaEvaluator formulaevaluator = (XSSFFormulaEvaluator)helper.createFormulaEvaluator();
WorkbookEvaluator workbookevaluator = formulaevaluator._getWorkbookEvaluator();
ValueEval valueeval = null;
valueeval = workbookevaluator.evaluate("TEXT(" + sheet.getSheetName() +"!C12, \"MMMM\")", null); //Sheet2!C12 must contain a date
if (valueeval instanceof StringValueEval) {
monthname = ((StringValueEval)valueeval).getStringValue();
}
System.out.println(monthname);
workbook.close();
}
}