Round column with formula

I have the following:

using (ExcelEngine excelEngine = new ExcelEngine())

            {

                IApplication application = excelEngine.Excel;

                application.DefaultVersion = ExcelVersion.Excel2013;

                IWorkbook workbook = application.Workbooks.Create(1);

                IWorksheet worksheet = workbook.ActiveSheet;

                worksheet[1, 1].Text = "Products";

                worksheet[1, 2].Text = "Rate";

                worksheet[1, 3].Text = "Quantity";


                worksheet[2, 1].Text = "Item1";

                worksheet[2, 2].Number = 200;

                worksheet[2, 3].Number = 2.25646;


                worksheet[3, 1].Text = "Item2";

                worksheet[3, 2].Number = 200;

                worksheet[3, 3].Number = 2.564654;

                application.EnableIncrementalFormula = true;

                worksheet.Columns[2].Formula = $"=ROUND(C1, 2)";

                string savePath = $"c:\\Test\\TestRound.xlsx";

                workbook.SaveAs(savePath);

            }


As you can see, I'm attempting to round column C but I get a circular reference error with my code above.  I'd like to round as opposed to adding a number format since formats don't change the underlying value.  Is there a way to do what i'm trying to do in the above?  Thanks!


1 Reply

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team October 22, 2021 10:28 AM UTC

Hi Matthew, 

Greetings from Syncfusion. 

This is the behavior of Microsoft Excel to show the circular reference warning, when you are reference to same cell in that cell formula. 

Example: C1 has formula referring to C1. 

We request you to use the formula in different cell to overcome the reported issue, if you are not comfortable with number format. Please find the modified code snippet below. 

Existing Code 
Modified Code 
worksheet.Columns[2].Formula = $"=ROUND(C1, 2)"; 
worksheet.Range["D1:D3"].Formula = $"=ROUND(C1, 2)"; 

Kindly try the suggestion and let us know if this helps. 

Regards, 
Keerthi. 


Loader.
Up arrow icon