//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); var worksheet = workbook.Worksheets[0]; worksheet.Range["A1"].Value = 1; worksheet.Range["A2"].Formula = "=A1"; worksheet.Range["A3"].Formula = "=SUM(A1, A2)"; //when get value, calc engine will first calculate and cache the result, then returns the cached result. var value_A2 = worksheet.Range["A2"].Value; var value_A3 = worksheet.Range["A3"].Value; //disable calc engine. workbook.EnableCalculation = false; //Dirty() method will mark the cached value of the workbook as dirty. workbook.Dirty(); //Change A1 value worksheet.Range["A1"].Value = 2; //Calculate() will not work, because of workbook.EnableCalculation is false. workbook.Calculate(); //it returns the wrong cached value because of calc engine is turned off. var value_A2_1 = worksheet.Range["A2"].Value; var value_A3_1 = worksheet.Range["A3"].Value; //enable calc engine. workbook.EnableCalculation = true; //Dirty() method will mark the cached value of the workbook as dirty. worksheet.Range["A2:A3"].Dirty(); //Calculate() method will calculate and cache the new result, it will return the cache value directly when get value later. worksheet.Range["A2:A3"].Calculate(); //it returns cache value directly, does not calculate again. var value_A2_2 = worksheet.Range["A2"].Value; var value_A3_2 = worksheet.Range["A3"].Value;
' Create a new Workbook Dim workbook As New Workbook Dim worksheet As IWorksheet = workbook.Worksheets(0) worksheet.Range("A1").Value = 1 worksheet.Range("A2").Formula = "=A1" worksheet.Range("A3").Formula = "=SUM(A1, A2)" 'when get value, calc engine will first calculate and cache the result, then returns the cached result. Dim value_A2 = worksheet.Range("A2").Value Dim value_A3 = worksheet.Range("A3").Value 'disable calc engine. workbook.EnableCalculation = False 'Dirty() method will mark the cached value of the workbook as dirty. workbook.Dirty() 'Change A1 value worksheet.Range("A1").Value = 2 'Calculate() will not work, because of workbook.EnableCalculation is false. workbook.Calculate() 'it returns the wrong cached value because of calc engine is turned off. Dim value_A2_1 = worksheet.Range("A2").Value Dim value_A3_1 = worksheet.Range("A3").Value 'enable calc engine. workbook.EnableCalculation = True 'Dirty() method will mark the cached value of the workbook as dirty. worksheet.Range("A2:A3").Dirty() 'Calculate() method will calculate and cache the new result, it will return the cache value directly when get value later. worksheet.Range("A2:A3").Calculate() 'it returns cache value directly, does not calculate again. Dim value_A2_2 = worksheet.Range("A2").Value Dim value_A3_2 = worksheet.Range("A3").Value ' save to an excel file workbook.Save("DirtyAndCalculation.xlsx")