How to prevent automatic formatting of the formula when setting Cell.Formula?

Workbook wb = new Workbook();
wb.Worksheets[0].Cells[0, 0].Formula = “=1 * 2”;
Console.WriteLine(wb.Worksheets[0].Cells[0, 0].Formula);

output is:
=1*2

expected result is:
=1 * 2

As shown above, the formulas have been formatted into a uniform style, but I would like to preserve the original input.

@Zhigang_Wang

To preserve spaces in the input formula expression, please enable the property FormulaSettings.PreservePaddingSpaces:

...
wb.Settings.FormulaSettings.PreservePaddingSpaces = true;
wb.Worksheets[0].Cells[0, 0].Formula = “=1 * 2”;
...

@Zhigang_Wang,

Aspose.Cells can preserve the spaces in formula while importing/setting/getting formulas accordingly. Please try our latest version or newer versions as the feature is supported in newer versions. For the compatibility of existing applications of most users, by default we do not preserve spaces when processing formulas unless you use the relevant APIs.

And, for loading from template file if you need to preserve spaces in the formulas, the code should be like the following:

LoadOptions opts = new LoadOptions(LoadFormat.Xlsx);
opts.PreservePaddingSpacesInFormula = true;
Workbook wb = new Workbook(path, opts);

Hope, this helps a bit.

Thank you for your reply, doing so indeed solved my problem.

Thank you very much for your reminder. I should indeed do the same processing when loading the file. You have helped me a lot.

@Zhigang_Wang,

You are welcome.
It’s good to know that the suggested code segments work for your needs.
If you have any further queries or issues, please feel free to write us back.