I am trying to add a data validation list to excel using OfficeOpenXml on D365fo but when the formula is assigned its throw an error.
worksheetTo = packageTo.get_Workbook().get_Worksheets().get_Item(1);
cellsTo = worksheetTo.Cells.get_Item(2, 2, totalRows, 2);
validation = worksheetTo.DataValidations.AddListValidation("B:B");
OfficeOpenXml.Datavalidation.Formulas.Contracts.IExcelDataValidationFormula formula = validation.Formula;
formula.ExcelFormula = "=Feuil2!$A:$A";
packageTo.Save();
file::SendFileToUser(streamTo, strDel(textFile, strLen(textFile) -4, 5) + "T.xlsx");
Error message:
Exception User-Unhandled
System.MethodAccessException: 'Attempt by method 'Dynamics.AX.Application.GMExcelTransformation.`run()' to access method 'OfficeOpenXml.DataValidation.ExcelDataValidationWithFormula`1<System._Canon>.set_Formula(System._Canon)' failed.'
validation variable is a OfficeOpenXml.DataValidation.ExcelDataValidationList;
I'm not sure what causes the exception in the question. The following code works, so it likely is caused by something not shown in the question.
The resulting Excel file looks like this: