I am using
Aspose.Cells 25.11.0.
.NET 9.0
using System.Collections;
using Aspose.Cells;
var input = @"C:\input-for-aspose.xlsx";
var output = @"C:\output.xlsx";
Workbook? wbc;
using (var ms = new FileStream(input, FileMode.Open))
wbc = new Workbook(ms, new LoadOptions(LoadFormat.Xlsx));
try
{
var engine = new CustomCalculationEngine
{
};
var opts = new CalculationOptions
{
CustomEngine = engine,
};
wbc.CalculateFormula(opts);
engine.SetCellValues(wbc);
wbc.CalculateFormula(opts);
wbc.Save(output);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
public class CustomCalculationEngine : AbstractCalculationEngine
{
private ArrayList _list = new ArrayList();
public override void Calculate(CalculationData data)
{
_list.Add(data.Worksheet.Index);
_list.Add(data.CellRow);
_list.Add(data.CellColumn);
_list.Add(data.Cell.Value);
try
{
SkipCalculation();
}
catch(Exception e)
{
}
}
public void SetCellValues(Workbook wb)
{
for (int i = 0; i < _list.Count; i+=4)
{
Cell cell = wb.Worksheets[(int)_list[i]].Cells[(int)_list[i + 1], (int)_list[i + 2]];
var value = _list[i + 3];
cell.Formula = null;
cell.R1C1Formula = null;
cell.Value = value; //here just re-assign its value to the cell itself, the assignment will remove the formula
}
}
}
We convert formulas into values after CalculateFormula. =XLOOKUP returns #VALUE and =IFNA formula breaks.
But if I start editing formula and press Enter everything works correctly.
input-for-aspose.zip (9.9 KB)
output.zip (10.1 KB)