When a formula contains new lines, it is parsed incorrectly as follows:
- When it is shared formula it breaks the imported expression and replaces it with #NAME? error.
- In normal case it simply preserves the formula string converting it to StringExpression.
Workaround: Remove the new lines in the formulas.
Have in mind that this code will fix the issue only for the second scenario, when the formula is not shared.
CellRange usedCellRange = workbook.ActiveWorksheet.UsedCellRange;
for (int row = usedCellRange.FromIndex.RowIndex; row < usedCellRange.ToIndex.RowIndex; row++)
{
for (int column = usedCellRange.FromIndex.ColumnIndex; column < usedCellRange.ToIndex.ColumnIndex; column++)
{
CellSelection cell = workbook.ActiveWorksheet.Cells[row, column];
ICellValue value = cell.GetValue().Value;
if (value.RawValue.Contains("\n"))
{
cell.SetValue(value.RawValue.Replace('\n', ' '));
}
}
}